2

I have a nested list in the following format:

[['john'],['jack','john','mary'],['howard','john'],['jude']...]

I want to find the first 3 or 5 indices of john that occurs in the nested list(since the list is really long) and return the indices like: (0,0),(1,1),(2,1) or in any format which is advisable.

I'm fairly new to nested list. Any help would be much appreciated.

Amal Sailendran
  • 341
  • 1
  • 2
  • 16
  • You can refer this link: https://stackoverflow.com/questions/6294179/how-to-find-all-occurrences-of-an-element-in-a-list?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Yogesh Nikam Apr 07 '18 at 08:33

1 Answers1

6

Question 1: Here is one way using a nested comprehension list. I will however look if there is a dupe.

nested_list = [['john'],['jack','john','mary'],['howard','john'],['jude']]

out = [(ind,ind2) for ind,i in enumerate(nested_list) 
                  for ind2,y in enumerate(i) if y == 'john']

print(out)

Returns: [(0, 0), (1, 1), (2, 1)]


Update: Something similar found here Finding the index of an element in nested lists in python. The answer however only takes the first value which could be translated into:

out = next(((ind,ind2) for ind,i in enumerate(nested_list) 
                       for ind2,y in enumerate(i) if y == 'john'),None)
print(out) # (0,0) 

Question 2: (from comment)

Yes this is quite easy by editing y == 'john' to: 'john' in y.

nested_list = [['john xyz'],['jack','john dow','mary'],['howard','john'],['jude']]

out = [(ind,ind2) for ind,i in enumerate(nested_list) 
                  for ind2,y in enumerate(i) if 'john' in y]
print(out)

Returns: [(0, 0), (1, 1), (2, 1)]


Question 3: (from comment)

The most efficient way to get the first N elements is to use pythons library itertools like this:

import itertools

nested_list = [['john xyz'],['jack','john dow','mary'],['howard','john'],['jude']]

gen = ((ind,ind2) for ind,i in enumerate(nested_list) 
                       for ind2,y in enumerate(i) if 'john' in y)

out = list(itertools.islice(gen, 2)) # <-- Next 2
print(out)

Returns: [(0, 0), (1, 1)]

This is also answered here: How to take the first N items from a generator or list in Python?


Question 3 extended:

And say now that you want to take them in chunks of N, then you can do this:

import itertools

nested_list = [['john xyz'],['jack','john dow','mary'],['howard','john'],['jude']]

gen = ((ind,ind2) for ind,i in enumerate(nested_list) 
                       for ind2,y in enumerate(i) if 'john' in y)

f = lambda x: list(itertools.islice(x, 2)) # Take two elements from generator

print(f(gen)) # calls the lambda function asking for 2 elements from gen
print(f(gen)) # calls the lambda function asking for 2 elements from gen
print(f(gen)) # calls the lambda function asking for 2 elements from gen

Returns:

[(0, 0), (1, 1)]
[(2, 1)]
[]
Anton vBR
  • 18,287
  • 5
  • 40
  • 46
  • Thank you so much, it answers my query. I also have a doubt if it would be possible to out the index of a string had it been in the following format. nested_list = [['john xyz'],['jack','john dow','mary'],['howard','john'],['jude']] , is it possible to return the index of all the places where john is mentioned? i.e, the same output : [(0, 0), (1, 1), (2, 1)] – Amal Sailendran Apr 07 '18 at 08:50
  • thanks soo much, i would have given you more upvotes if i could. Thanks for help as i'm new to this. Unlike some people who prefer sarcastic comments for an honest query. :) – Amal Sailendran Apr 07 '18 at 08:57
  • 1
    @AmalSailendran Glad I could help. However when the answer you look for already has been answered the community prefer finding the dupes. It keeps the question amount low. In SO you can accept answers but upvote (+1) or downvote (-1) using the arrows. Thank you. – Anton vBR Apr 07 '18 at 08:59
  • 1
    i have accepted the answer :). I have one last query, is it possible to limit the number of outputed indices to maybe like initial 5? as my list is like 2000 of them – Amal Sailendran Apr 07 '18 at 09:00
  • 1
    @AmalSailendran Yes well. There are many ways of doing that. The easiest is to add `[:5]` after out, like this: `out[:5]`. This is called slicing in python. – Anton vBR Apr 07 '18 at 09:11