imdumbplshelp :
I'm pretty new to python, and I'm stuck on a loop/list problem. I have two lists and want to search through the lists to find matching elements and add an item from that list to a new list.
Here's my code:
newList = []
elements = [["Wood", "elem1"], ["Stone", "elem2"], ["Concrete", "elem3"]]
nameNum = [["Wood", "2316"], ["Concrete", "3360"], ["Stone", "2785"]]
The lists can be longer and not always the same length, but just using 3 examples.
What I actually need is newList = ["2316", "2785", "3360"]
.
P47 R1ck :
elements = [["Wood", "elem1"], ["Stone", "elem2"], ["Concrete", "elem3"]]
nameNum = [["Wood", "2316"], ["Concrete", "3360"], ["Stone", "2785"]]
new_list =[]
for element in elements:
for num in nameNum:
if num[0] in element:
new_list.append(num[1])
break
print(new_list)
Looping 2 times in an nested list to extract first the element from elements, then second loop to extract value from nameNum
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=291491&siteId=1