I'm using python 3
I have a list of different tuples. the first value of every tuple is always the same but the rest vary. How can I search through the list after the tuple containing the first value and then replacing the entire tuple whit an new one inside the list?
ex:
this is my tuple: (122, 23, 24, 9)
this tuples are inside the list, the first value is always the same but the rest vary how can i find the tuple inside of the list that got a first value equal to mine tuple "122" and replace it whit my whole tuple
(125, 34, 35, 21)
(122, 341, 5, 27)
(124, 31, 51, 7)
l = [(125, 34, 35, 21), (122, 341, 5, 27), (124, 31, 51, 7)]
t = (122, 23, 24, 9)
out = [t if e[0] == t[0] else e for e in l]
print(out)
This prints out:
[(125, 34, 35, 21), (122, 23, 24, 9), (124, 31, 51, 7)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With