0

I want to use list comprehension to change an element within a nested list to float.

lines = [[1, '74.37000326528938', 'Psychologist'], [2, '67.49686206937491', 'Psychologist'], [3, '74.92356434760966', 'Psychologist']]
>>> lines = [[line[0]] + [float(x) for x in line[1]] + [line[2]] for line in lines]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: .

I also don't understand why this would work for a different example:

mylist = [['1','2','3'],['5','6','7']]
datl3 = [[int(x) for x in line[0]] + line[1:] for line in mylist]
datl4 = [[line[0]] + [float(x) for x in line[1]] + [line[2]] for line in datl3]
>>> datl4
[[1, 2.0, '3'], [5, 6.0, '7']]

I think it has to do with decimal points, because the code won't work for this example:

mylist = [['1','2.555','3'],['5','6.777','7']]
datl3 = [[int(x) for x in line[0]] + line[1:] for line in mylist]
datl4 = [[line[0]] + [float(x) for x in line[1]] + [line[2]] for line in datl3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: .
3
  • 2
    Why are you building a new list? Shouldn't for l in lines: l[1] = float(l[1]) work? Commented Sep 30, 2018 at 0:30
  • @coldspeed Would the for loop only change the element l[1]? Commented Sep 30, 2018 at 0:32
  • No, since l is a reference to an inner list, it ends up modifying the original lines. Commented Sep 30, 2018 at 0:33

3 Answers 3

1

You were close but the problem was that you were using a for loop on line[1] which is a single string. Hence, you were looping through each element/character of your float number. When you reach the decimal . you get the ValueError: could not convert string to float: .. Your second example works because there you just have a single digit strings 2 and 6 without any decimal.

If you stick to your code, you just need to replace the index line[1] by line[1:2] as follows where you slice

lines = [[line[0]] + [float(x) for x in line[1:2]] + [line[2]] for line in lines]

However, a more concise way is following where you avoid an unnecessary intermediary for loop by directly accessing the second element of the sublists. You don't need to create three sublists and then add them using +

lines = [[line[0], float(line[1]), line[2]] for line in lines]

Output

[[1, 74.37000326528938, 'Psychologist'],
 [2, 67.49686206937491, 'Psychologist'],
 [3, 74.92356434760966, 'Psychologist']]
Sign up to request clarification or add additional context in comments.

3 Comments

for x in line[1:2] is a complicated way of getting line[1].
@Code-Apprentice: I agree but my aim here was to tell the OP what is going wrong in his/her approach. Reading all three answers, I feel we all are telling the same thing about the decimal
You could add to your question by showing how to write the final solution in a more clear and concise way.
1

The problem is that this line is trying to do too many things at once:

datl4 = [[line[0]] + [float(x) for x in line[1]] + [line[2]] for line in datl3]

The problem occurs in the inner list comprehension:

[float(x) for x in line[1]]

But what is line[1]? It comes from the iteration for line in datl3. So let's see what values it has:

>>> [line[1] for line in datl3]
['2.555', '6.777']

So when you do [float(x) for x in line[1]] you are iterating over the characters of each numerical string..."2" then "." then "5" then "5" then "5". But "." is not a valid float.

I think what you want to do instead is just convert line[1] to a float:

lines = [[line[0], float(line[1]), line[2]] for line in lines]

Comments

0

It can be done with list comprehension just need to set it up properly, check the str for a decimal, if so make that item an int

l = [[float(j) if '.' in str(j) else j for j in i] for i in l]
[[1, 74.37000326528938, 'Psychologist'], [2, 67.49686206937491, 'Psychologist'], [3, 74.92356434760966, 'Psychologist']]

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.