Reputation: 331
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.
Upvotes: 2
Views: 1486
Reputation: 18906
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)]
[]
Upvotes: 6