I have a list of lists
list_1 = [['good', 2, 2], ['bad', 2, 2], ['be', 1, 1], ['brown', 1, 3]]
I would like to add new element to the inner list by summing the two numbers. So my list should look like
list_1 = [['good', 2, 2, 4], ['bad', 2, 2, 4], ['be', 1, 1, 2], ['brown', 1, 2, 3]]
How do I add开发者_运维知识库 insert new element into list by adding a column? Thanks for your help!
for lst in list_1:
lst.append(lst[1]+lst[2])
- Iterate over your list of lists.
- For each list in your list of lists,
- Compute your new element, and append it to the list.
list_1 = [['good', 2, 2], ['bad', 2, 2], ['be', 1, 1], ['brown', 1, 3]]
print(list_1)
for i in range(len(list_1)):
list_1[i]+=[list_1[i][1]+list_1[i][2]]
print(list_1)
精彩评论