+ 2

List Comprehension in Python.

I have a Nested list in python which is structured like this.. [ [1,2,3] [4,5,6] [7,8,9] ] I want to use List Comprehension specifically, to iterate over all the items in the list and add 0.1 to them and produce a new list. Any idea on how can i do that.

27th Apr 2019, 4:26 AM
Velan
Velan - avatar
2 odpowiedzi
+ 11
l = [ [1,2,3], [4,5,6], [7,8,9], ] n = [[j+0.1 for j in i] for i in l] It might be easier to use a numpy array though: https://code.sololearn.com/c9ySLRIO8yFY/?ref=app
27th Apr 2019, 4:43 AM
Anna
Anna - avatar
+ 1
Oh okay. So the trick was to loop over row in the outer for loop and column in the inside. I used to get error saying inner iterator variable isn't declared. Great help ! Thank you. Solved my problem.
27th Apr 2019, 4:48 AM
Velan
Velan - avatar