1
sublist = [[32,999,15,329,679],[1738,100,55,1800,1469],["bruges","manchester","bristol","edinburgh","barcelona"]["Vienna","Stockholm","Berlin",Prague,"Dublin"]]

These sublists purposely have a mix of strings and integers for example but with 6 more sublists. Is there a way to search for example "Vienna" and return 4 as in "Vienna" is in the fourth sublist.

Many thanks

  • This might help you: https://stackoverflow.com/questions/21262994/find-index-of-element-in-sub-sub-list – Melkozaur Aug 10 '20 at 09:54

3 Answers3

0

Here is a possible solution:

result = next((i for i, lst in enumerate(sublist) if 'Vienna' in lst), None)

In your specific case 3 is returned. If no suitable sublist is found (i.e., no sublists contains 'Vienna'), then None is returned.

Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50
  • thank you for this , this is been a great help, however i made a mistake and forgot that indexes start at zero making Vienna return 3, is there a way to edit your result variable to return 3 instead outside of just result minus 1. Cheers –  Aug 10 '20 at 10:14
  • @HusslinHustla Fixed, now the code correctly returns 3 as a result – Riccardo Bucco Aug 10 '20 at 10:15
0

If you only have one level, you can just loop through this list and check whether the element is in this list, using the in keyword.

If, however, you have nested sublists with several levels, you might want to use a recursive function to scan all of them. The function finder() below scans through all nested lists and returns the indices of the lists in which the element was first encountered:

sublist = [[32,999,15,329,679],
           [1738,100,55,1800,1469],
           ["bruges","manchester","bristol","edinburgh","barcelona"],
           ["Vienna","Stockholm","Berlin","Prague","Dublin"],
           ["Dog",["Cat","Parrot"]]]

def finder(nested,element):
    for i in range(len(nested)):
        if type(nested[i])==list:
            f = finder(nested[i],element)
            if type(f)==list:
                return [i]+f
        elif nested[i]==element:
            return [i]

print(finder(sublist,"Vienna"))
print(finder(sublist,"Parrot"))


#Output:
#[3, 0]
#[4, 1, 1]

The output means that "Vienna" is in list 3, the 0th element. The element "Parrot" was in list 4, within list 1, the 1th element.

Martin Wettstein
  • 2,771
  • 2
  • 9
  • 15
0

You can use the enumerate function to elegantly solve your problem. However, keep in mind that Python uses 0-based indexing for arrays, so if you want to return 4 for "fourth sublist", you will need to add 1.

def find(nest, elem):
  for idx, lst in enumerate(nest):
      if elem in list:
          return idx

sublist = [
  [32,999,15,329,679],
  [1738,100,55,1800,1469],
  ["bruges","manchester","bristol","edinburgh","barcelona"],
  ["Vienna","Stockholm","Berlin","Prague","Dublin"]]
print(find(sublist, "Vienna"))

To return 4:

def find(nest, elem):
  for idx, lst in enumerate(nest):
      if elem in list:
          return idx + 1
Kevin Yu
  • 26
  • 4