You can simply browse the list looking for the tuple with the desired letter, and replace the entire tuple (you cannot change the tuples), breaking out of the loop when you find the necessary element. For instance,
lst = [(1, 'q'), (2, 'w'), (3, 'e'), (4, 'r')] def update(item, num): for i, t in enumerate(lst): if t[1] == item: lst[i] = num, item break update('w', 6) print(lst)
Exit
[(1, 'q'), (6, 'w'), (3, 'e'), (4, 'r')]
However, you should seriously consider using a dictionary instead of a list of tuples. A dictionary search is much more efficient than performing a linear scan through a list.
source share