-1

I was wondering if it was possible to do the following:

very_special = ["special"]
my_list = ["stuff", very_special]
if "special" in my_list:
     print ("Found it")

I was wondering if you could do this in some way, having the code look through the whole thing, including the lists inside the list.

JohnyNich
  • 1,221
  • 3
  • 14
  • 23

7 Answers7

3

You can achieve this by using recursion. Something along these lines:

very_special = ["special"]
my_list = ["stuff", very_special]

def nested_list_search(lst, term):
    for ele in lst:
        if isinstance(ele, list):
            nested_list_search(ele, term)
        else:
            if ele == term:
                print("Found it")

nested_list_search(my_list, "special")
>>> Found it
Zailef
  • 677
  • 1
  • 10
  • 13
0

Other user here suggested recursion...It might be an overkill for what he asked for. You can just look over items as well.

very_special = ["special"]
my_list = ["stuff", very_special]

if "special" in my_list:
     print("Found it")

for item in my_list:
    if isinstance(item, list) and "special" in item:
        print("Found it nested inside")
Almog Cohen
  • 1,283
  • 1
  • 13
  • 12
0

If the nesting is only one level deep, you can loop over each element in the outer list:

for elem in my_list:
  if elem == "special" or (isinstance(elem, collections.Iterable) and "special" in elem):
    print("Found it.")

If you want to handle arbitrary nesting, you'll need to flatten them first (like Flatten (an irregular) list of lists):

def flatten(lst):
  for elem in lst:
    if isinstance(elem, collections.Iterable) and not isinstance(elem, (str, bytes)):
      yield from flatten(elem)
    else:
      yield elem

if "special" in flatten(my_list):
    print ("Found it")
Community
  • 1
  • 1
temoz
  • 5
  • 2
0

For this situation you need some work around, Here is a snippet to achieve it. Same way you can write your own to work on.

very_special = ["special", "v", ["xyz", "we"]]
my_list = ["stuff", "n", very_special]

def aggregate(newList):
    opList = []
    for item in newList:
        if list is type(item):
             opList += aggregate(item)  
        else:
            opList.append(item)

    return opList 


if "special" in aggregate(my_list):
     print ("Found it")
Rakesh Kumar
  • 4,319
  • 2
  • 17
  • 30
0

Using the original suggestion way will not work. The membership operator in usually behavior is to check if x is member of y without going deeper to check if x is also member of any member of y.

For this case, for this specific example, here is a very specific way to solve this.

very_special = ["special"]
my_list = ["stuff", very_special]

for element in my_list:
    if "special" in element:
        print ("Found it")
Scantlight
  • 253
  • 3
  • 13
0

Depending on your needs, the following approach might be appropriate:

import itertools

very_special = ["special"]
my_list = ["stuff", very_special]

if "special" in itertools.chain.from_iterable(my_list):
     print ("Found it")

This makes use of Python's chain() function which gets chained inputs from a single iterable argument that is evaluated lazily.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
-1

Using the + operator

very_special = ["special"]
my_list = ["stuff"] + very_special
if "special" in my_list:
    print ("Found it")
ayvazj
  • 3,943
  • 1
  • 14
  • 14