Issue
How to replace the first value in a list of lists with a new value?
list_of_lists = [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]
new_values = [1, 2, 3]
for i in list_of_lists:
for n in range(len(new_values)):
list_of_lists[n][0] = new_values[n]
Desired output
list_of_lists = [[1, "b" ,"c"], [2, "e", "f"], [3, "h", "i"]]
Solution
This one is actually quite simple
list_of_lists = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
new_values = [1, 2, 3]
for sub_list in list_of_lists:
sub_list[0] = new_values.pop(0)
This iterates over the lists and removes the first value of new_values each time.
Answered By - Adam Luchjenbroers
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.