Given a list like the one below, I want to be able to check what the second element of the nested list is given the first element of the nested list. (There would be no repeated elements of the same index in different nested lists - meaning: there wouldn't be "apple" as the 1st element of more than one nested list) For example: I want to get the result "peach" knowing that "orange" is the 1st element of the nested list, but NOT knowing that ["orange", "peach"] is the second element of list Example.

Example = [["apple", "banana"], ["orange", "peach"], ["strawberry", "blueberry"]]

I've tried using the .index() function but it only works for a whole element of the list (which in this case would be ["orange", "peach"] - it tells me that "orange" isn't an element of the list if I try Example.index("orange").

5 Answers

1
Community On Best Solutions

Something like the following will give you a list of all of the second elements of sub lists whose first element matches a given search word/element; not sure if this is exactly what you're looking for:

nested_list = [["apple", "banana"], ["orange", "peach"], ["strawberry", "blueberry"]]
search_word = 'orange'
results = [sub_list[1] for sub_list in nested_list if sub_list[0] == search_word]

Result:

['peach']

If you also require the index of the sub list with a matching first index you could make the following change:

nested_list = [["apple", "banana"], ["orange", "peach"], ["strawberry", "blueberry"]]
search_word = 'orange'
results = [(i, nested_list[i][1]) for i in range(len(nested_list)) if nested_list[i][0] == search_word]

Result:

[(1, 'peach')]
1
alireza yazdandoost On

Try this:

Example = [["apple", "banana"], ["orange", "peach"], ["strawberry", "blueberry"]]
q = "orange"
res = [m[1] for m in Example if m[0]==q][0]
if len(res) > 0:
    print(res[0])
#Result: "peach"
0
Gerges On

Here is a way to do it. Loop through each element in the outer list, search for 'orange' and ask for forgiveness if you don't see it in current item. You get None if you don't find 'orange' or if 'orange' was the last element in the inner list.

This returns first finding of 'orange', but can be modified to return a list of all findings.

element = None
for item in Example:
    try:
        idx = item.index('orange')
        if idx < len(item)-1:
            element = item[idx+1]
        break
    except ValueError:
        pass
1
Mark Tolonen On

From the description you should be using a dictionary. The first elements are unique keys and the latter are values. The lookup will be much faster.

dict() can be constructed using two-item sublists:

>>> Example = [["apple", "banana"], ["orange", "peach"], ["strawberry", "blueberry"]]
>>> d = dict(Example)
>>> d['orange']
'peach'
0
Community On
Example = [["apple", "banana"], ["orange", "peach"], ["strawberry", 
"blueberry"], 
       ["mango", "ananas"], ["limon", "blueberry"]]
target = "mango"
index1 = 0
index2 = 0
for i in Example:
index1+=1
for j in i:
    index2+=1 
    if(not (index2%2 == 0) and j == target): #check only first  element of 
#nested list
        print(Example[index1-1][1])

If i get it right this will do the job.