3 views
in Python
closed

Consider the below list 1,

list1 = [('my', '1.2.3', 2),('name', '9.8.7', 3)]

I also need to get a new list2 like below,

list2 = [('my2', 2),('name8', 3)]

In the first step, I'm attempting to combine the first two elements inside a tuple,

for i,j,k in list1:

#print(i,j,k)

x = j.split('.')[1]

y = str(i).join(x)

print(y)

But I get this,

2

8

But, What I expect

my2

name8

I don't know, where I went wrong?

closed

by (15.4k points)
selected

To resolve the issue in your code, you need to make a small modification. The problem lies in the line y = str(i).join(x). Instead, you should use the join() method on an empty string and pass i and x as arguments to concatenate them correctly.

Here's the updated code:

list1 = [('my', '1.2.3', 2), ('name', '9.8.7', 3)]

list2 = []

for i, j, k in list1:

x = j.split('.')[1]

y = ''.join([str(i), x])  # Concatenate i and x using ''.join()

list2.append((y, k))

print(list2)

This will give you the expected output:

[('my2', 2), ('name8', 3)]

In the revised code, y = ''.join([str(i), x]) creates a new string by concatenating i and x without any separator. The resulting string is then added to list2 along with the corresponding value of k.
by (26.4k points)

You can try,

y=str(i)+str(x)

It will work

Join the python online course fast, to learn python concepts in detail and get certified.

For more details, do check out the below video tutorial...
by (25.7k points)
The issue in your code lies in the line y = str(i).join(x). The join() method is being called on the string representation of i, which is causing unexpected behavior. Instead, you should use the join() method on an empty string and pass i and x as arguments to concatenate them together.

Here's the corrected code:

list1 = [('my', '1.2.3', 2), ('name', '9.8.7', 3)]

list2 = []

for i, j, k in list1:

x = j.split('.')[1]

y = ''.join([str(i), x])  # Concatenate i and x using ''.join()

list2.append((y, k))

print(list2)

This will produce the expected output:

[('my2', 2), ('name8', 3)]

In the corrected code, y = ''.join([str(i), x]) creates a new string by concatenating i and x without any separator in between. The resulting string is then appended to list2 along with the corresponding value of k.
by (19k points)
To fix the issue in your code, you need to modify the line y = str(i).join(x). The problem arises because you're calling the join() method on the string representation of i instead of using an empty string as the separator.

Here's a corrected code:

list1 = [('my', '1.2.3', 2), ('name', '9.8.7', 3)]

list2 = []

for i, j, k in list1:

x = j.split('.')[1]

y = ''.join([str(i), x])  # Concatenate i and x using an empty string as the separator

list2.append((y, k))

print(list2)

This will give you the expected output:

[('my2', 2), ('name8', 3)]

In the updated code, y = ''.join([str(i), x]) creates a new string by concatenating i and x without any separator. The resulting string is then appended to list2 along with the corresponding value of k.