Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace a tuple inside of a list by it's first entry

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)
like image 567
user1229391 Avatar asked Dec 29 '25 20:12

user1229391


1 Answers

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)]
like image 132
NPE Avatar answered Jan 01 '26 12:01

NPE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!