Home > Net >  Python: change values in list tuple from list with the same indexes
Python: change values in list tuple from list with the same indexes

Time:10-11

I am trying to replace a value in a list tuple in a list. The index number of the list tuple corresponds to the list number.

list_tuple = [('BHS', '2018-05-24_12:14:51.985'), ('CLS', '2018-05-24_12:09:41.921'), ('CSR', '2018-05-24_12:03:15.145'), ('DRR', '2018-05-24_12:14:12.054'), ('EOF', '2018-05-24_12:17:58.810')]

list_values = [153.179, 112.829, 112.95, 287.987, 546.972]


example = [('BHS', 153.179), ('CLS', 112.829), ('CSR', 112.95), ('DRR', 287.987), ('EOF', 546.972)]

CodePudding user response:

The fact that you want to combine ingredients that are at the same index means that you want to iterate over the two lists in parallel. Python's zip function is defined for such parallel iteration:

example = [(t[0],v) for t,v in zip(list_tuple,list_values)]

produces the list that you are trying to construct.

  • Related