So, I got this question on Coursera’s Python course through Runestone Academy:
Use list comprehension to create a list called lst2 that doubles each element in the list, lst.
lst = [[“hi”, “bye”], “hello”, “goodbye”, [9, 2], 4]
Here’s my code
lst2 = [l + l for l in lst]
print(lst2)
Output:
[[‘hi’, ‘bye’, ‘hi’, ‘bye’], ‘hellohello’, ‘goodbyegoodbye’, [9, 2, 9, 2], 8]
I am not understanding why all of it “doubled” except for the last digit of lst, 4, where it became 8.
I expected for the output to be:
[[‘hi’, ‘bye’, ‘hi’, ‘bye’], ‘hellohello’, ‘goodbyegoodbye’, [9, 2, 9, 2], 4, 4]
Why is this the case?
labshepherd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.