Skip to content
Advertisement

How to change one element in a sublist using list comprhension

I am worrying in python, and I’ve a list composed by 8 sublists, made in this way: [[value1,value2,value3], …].

I want to change to “False” only the third value (value3) in all sublists using a list comprhension, but my code does not work, and a I don’t know why.

[[a, b, False] for a, b, _ in athlete_session_el['top_bottom_first']]

athlete_session_el['top_bottom_first'] is the list of sublists.

Advertisement

Answer

The following works:

athlete_session_el = {}
athlete_session_el['top_bottom_first'] = [[1,2,3],[1,2,3],[1,2,3]]

print([[sublist[0], sublist[1], False] for sublist in athlete_session_el['top_bottom_first'] ])

[[1, 2, False], [1, 2, False], [1, 2, False]]
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement