Use a list comprehension and the int() function to convert all values to integers

Screen Link:

My Code:

with open('identifiers.txt') as file:
    values = list(file)
    for value in values:
        values = [int(value)]

I expected the output to be identical to the single-line solution in the answer.

values = [int(value) for value in values]

But the output was 31 instead of the desired 34.

Why the difference?


Hi @55cmb55:

As seen in the output of the variables with your code:

and the output of the variables with the suggested solution:

As you can see, the values list in your instance consists of 1 element which is equivalent of using the sum() function, which adds up all the elements in the list instead of a list of integers (which is the expected output to be fed into the minimum_required_bits() function). You are advised to follow the usual list comprehension syntax as described here.

1 Like