0
a = [[3, (1, 2, 3, 4, 5)], [3, (5, 4, 3, 2, 1)]]

b = [[3, (18, 24, 21, 2, 3)], [3, (3, 4, 76, 7, 8)]]

How can I access (1, 2, 3, 4, 5), (5, 4, 3, 2, 1), (18, 24, 21, 2, 3) and (3, 4, 76, 7, 8)?

I have to search for this elements in different lists.

For example: Is (3, 4, 76, 7, 8) in list a and if yes, what are the concrete numbers for this element?

( In that case [3, (3, 4, 76, 7, 8)] ).

Thanks in advance.

rdas
  • 20,604
  • 6
  • 33
  • 46
Dan Ru
  • 7
  • 5
  • Your question is unclear, do you mean to ask "how can I select sublists from a list where the sublist contains some specific element?" - i.e. `[x for x in a if (3, 4, 76, 7, 8) in x]`? – Grismar Jun 05 '20 at 01:42
  • @Grismar Yes :-). I am not so long in this programing thing. Sorry. – Dan Ru Jun 05 '20 at 01:45
  • Does this answer your question? [Python search in lists of lists](https://stackoverflow.com/questions/1156087/python-search-in-lists-of-lists) – Grismar Jun 05 '20 at 01:47
  • @Grismar The answer from Samwise makes it. Its perfect for me. But thank you too for your help. – Dan Ru Jun 05 '20 at 01:51

1 Answers1

0
>>> a = [[3, (1, 2, 3, 4, 5)], [3, (5, 4, 3, 2, 1)]]
>>> b = [[3, (18, 24, 21, 2, 3)], [3, (3, 4, 76, 7, 8)]]
>>> [x for x in a if (3, 4, 76, 7, 8) == x[1]]
[]
>>> [x for x in b if (3, 4, 76, 7, 8) == x[1]]
[[3, (3, 4, 76, 7, 8)]]
>>> [x for x in a + b if (3, 4, 76, 7, 8) == x[1]]
[[3, (3, 4, 76, 7, 8)]]
Samwise
  • 68,105
  • 3
  • 30
  • 44