Posts

Showing posts from February 6, 2019

How to modify last element of Python list in list comprehension?

Image
1 I have a code segment like following which throws an invalid syntax error: lst = [0, 0, 0] new_lst = [lst[-1] = i + 1 for i in range(3)] Basically, I want new_lst to be list of lists, where every list will be lst but the last element changed. Any ideas how to do this in Python? python list share | improve this question edited Nov 14 '18 at 11:51 aydow 2,360 1 10 26 asked Aug 20 '18 at 11:09 terett terett 226 1 7