Reputation: 39
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")
.
Upvotes: 0
Views: 808
Reputation: 180
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.
Upvotes: 0
Reputation: 177901
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'
Upvotes: 2
Reputation: 161
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')]
Upvotes: 1
Reputation: 6519
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
Upvotes: 0
Reputation: 774
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"
Upvotes: 1