iTookAPill
iTookAPill

Reputation: 49

search tuple with a list and return new tuple with only elements of the list

I have a tuple and a list, and I want to search the tuple with the list and return an new tuple.

oldtup = [(8, 46), (11, 65), (11, 78), (42, 11), (43, 78), (48, 81), (50, 44), (55, 7), (81, 80), (92, 17), (98, 45), (99, 9), (99, 45)]

listToSearchTuple = (8, 42, 43, 99)

newtup = [(8, 46), (42, 11), (43, 78), (99, 9), (99, 45)]

I am only interested in searching the first item of the tuple.

I have success with one element, but not a list as explained here: Find an element in a list of tuples

Unfortunately, I hit the wall with this and I am lost.

Upvotes: 0

Views: 115

Answers (1)

zondo
zondo

Reputation: 20346

It's very simple:

newtup = [tup for tup in oldtup if tup[0] in listToSearchTuple]

Upvotes: 1

Related Questions