简体   繁体   中英

How to update list of tuple

list_data = [(4,5),(6,7)] 
output should be [(4,5),(11,7)]

So i want to update 6 to 11.

Here list_data is list of tuple and tuple is immutable data type, so we can not update tuple. but my requirement is to update. Any help will be appreciable.

Tuples are immutable , try using a nested list instead!

That way you will be able to .append() or .pop() any element on any list.

Just replace. Lists are mutable so you can do just that.

for i,(a, b) in enumerate(list_data):
  if a == 6:
    list_data[i] = (11, b)

Convert the tuple to list and change the value and then update the result.

list_data = [(4,5),(6,7)] 
tpl = list(list_data[1])
tpl[0] = 11
list_data[1] = tuple(tpl)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM