Home > OS >  python: list of tuples search
python: list of tuples search

Time:10-28

Need some help here

num = [(1,4,5,30,33,41,52),(2,10,11,29,30,36,47),(3,15,25,37,38,58,59)]

if the last 6 digits are located to return the first digit.

example if finds 10,11,29,30,36,47 return 2

CodePudding user response:

You can use next with a conditional generator expression:

num = [(1,4,5,30,33,41,52),(2,10,11,29,30,36,47),(3,15,25,37,38,58,59)]
search = 11

next(first for first, *rest in num if search in rest)
# 2

CodePudding user response:

You can use next similair to user's approach:

num = [(1,4,5,30,33,41,52),(2,10,11,29,30,36,47),(3,15,25,37,38,58,59)]
to_find = [10,11,29,30,36,47]

print(next(n for n, *nums in num if nums == to_find))

2
  • Related