-5

My list is like this and following is my code but it does not work.

 templist = [('375ml Bott 24', '6.0', '10.0', '60.0'), ('CHINA WINE', '4.0', '16.0', '64.0')]
 for i in range(len(tempList)):
     tempList[i][1] = int(float(tempList[i][1]))
     tempList[i][2] = int(float(tempList[i][2]))
     tempList[i][3] = int(float(tempList[i][3]))
2

6 Answers 6

2

Using list comprehension with unpacking:

[(s, *map(float, fs)) for s, *fs in templist]

Output:

[('375ml Bott 24', 6.0, 10.0, 60.0), ('CHINA WINE', 4.0, 16.0, 64.0)]

Explanation:

  • for s, *fs in ...: for the elements of tuples in templist, take first item (str) as s and rest as fs.
  • (s, *map(float, fs)): keep the first item as it is, and convert fs into float then unpacking into a new tuple.
Sign up to request clarification or add additional context in comments.

Comments

1

You're trying to change a tuple but you can't since it is immutable. What you can do instead is convert to a list, change the values and convert back to a tuple:

templist = [('375ml Bott 24', '6.0', '10.0', '60.0'), ('CHINA WINE', '4.0', '16.0', '64.0')]

for i,tup in enumerate(templist):
     l = list(tup)
     l[1] = int(float(l[1]))
     l[2] = int(float(l[2]))
     l[3] = int(float(l[3]))
     templist[i] = tuple(l)

print(templist)

Output:

[('375ml Bott 24', 6, 10, 60), ('CHINA WINE', 4, 16, 64)]

You could also use a list comprehension to do the same:

for i,tup in enumerate(templist):
     l = list(tup)
     l[1:] = [int(float(x)) for x in l[1:]]
     templist[i] = tuple(l)

Comments

0

try :

tempList = [['375ml Bott 24', '6.0', '10.0', '60.0'], ['CHINA WINE', '4.0', '16.0', '64.0']]
for i in range(len(tempList)):
    tempList[i][1] = int(float(tempList[i][1]))
    tempList[i][2] = int(float(tempList[i][2]))
    tempList[i][3] = int(float(tempList[i][3]))
print(tempList)

1 Comment

the list type cannot change.
0

Do this: Convert the Tuple inside the List into Lists and convert string to Float.

templist = [('375ml Bott 24', '6.0', '10.0', '60.0'), ('CHINA WINE', '4.0', '16.0', '64.0')]
result =[]
for i in range(len(templist)):
    lis=list(templist[i])
    lis[1] = float(lis[1])
    lis[2] = float(lis[2])
    lis[3] = float(lis[3])
    result.append(lis)
print(result)

Your new list is result

Comments

0

We cant change the type inside a tuple but we can create a new tuple based on the old templist. Try this:

templist = [('375ml Bott 24', '6.0', '10.0', '60.0'), ('CHINA WINE', '4.0', '16.0', '64.0')]
newlist = []
for item in templist:
    z = []
    for i in item:
        try:
            z.append(int(float(i)))
        except:
            z.append(i)
    newlist.append(tuple(z))

print(newlist)

OUTPUT:

[('375ml Bott 24', 6, 10, 60), ('CHINA WINE', 4, 16, 64)]

Comments

0

You have a list of tuples. Elements of a list can be changed, it can have different tuple but tuple is immutable. you should have list of lists as mentioned below :

tempList = [['375ml Bott 24', '6.0', '10.0', '60.0'], ['CHINA WINE', '4.0', '16.0', '64.0']]


In this case :
templist = [('375ml Bott 24', '6.0', '10.0', '60.0'), ('CHINA WINE', '4.0', '16.0', '64.0')]

tempList[0] = tempList[1] # can be done because it edit the list which is mutable
tempList[0][1] = tempList[1][1] # can't be done because it edit the tuple which is immutable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.