Home > Software engineering >  How to iterate through a list and a dictionary in a nested for loop
How to iterate through a list and a dictionary in a nested for loop

Time:12-09

I am using python 3.9 and attempting to take information from a python list and a python dictionary and iterate through it for a loop. The correct email address will be taken from the dictionary depending on the unit.

When I execute my code, it loops three times over each member of the list, and I don't know how to make it not do that. I believe the initial for loop is executed ok and gets the metal, but is it the second loop that's making it run three times per item, and how do I fix it?

I realize this is pretty noddy, but I must be making fundamental mistakes somewhere, and after trying to figure it out for the last 5 hours, it's now time to ask for some help.

# Dictionary of metals and email addresses
metals = {
    'gold':'[email protected]',
    'silver':'[email protected]',
    'platinum':'[email protected]',
}

# Variable holding some string data
gold = """
Gold is a chemical element with the symbol Au and atomic number 79, 
"""
silver = """
Silver is a chemical element with the symbol Ag and atomic number 47. 
"""
platinum = """
Platinum is a chemical element with the symbol Pt and atomic number 78.
"""

units = [gold, silver, platinum]

# What I want to do is have a loop where it takes the item in the list
#, for example, gold, matches it with the key to that in the dictionary, thereby
# enabling me to send gold to [email protected], silver to [email protected], and platinum to
# 3gmail.com

for unit in units:
    for metal in metals.items():
        if unit == gold:
            email_address = metals.get('gold')
            print(email_address)
        elif unit == silver:
            email_address = metals.get('silver')
            print(email_address)
        elif unit == platinum:
            email_address = metals.get('platinum')
            print(email_address)
        else:
            print('No Match')

# just some code to print out various bits of information
# Print our Dictionary Keys
for k in metals.keys():
    print('Keys: '   k)

# Print our dictionary Values
for v in metals.values():
    print('Values: '   v)

# print out the values held in our list
for item in units:
    print('Items: '   item)

and here is the output:

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
Keys: gold
Keys: silver
Keys: platinum
Values: [email protected]
Values: [email protected]
Values: [email protected]
Items: 
Gold is a chemical element with the symbol Au and atomic number 79, 

Items: 
Silver is a chemical element with the symbol Ag and atomic number 47. 

Items: 
Platinum is a chemical element with the symbol Pt and atomic number 78.

CodePudding user response:

Just remove the inner for loop, changing this:

for unit in units:
    for metal in metals.items():
        if unit == gold:
            email_address = metals.get('gold')
            print(email_address)
        elif unit == silver:
            email_address = metals.get('silver')
            print(email_address)
        elif unit == platinum:
            email_address = metals.get('platinum')
            print(email_address)
        else:
            print('No Match')

to this:

for unit in units:
    if unit == gold:
        email_address = metals.get('gold')
        print(email_address)
    elif unit == silver:
        email_address = metals.get('silver')
        print(email_address)
    elif unit == platinum:
        email_address = metals.get('platinum')
        print(email_address)
    else:
        print('No Match')

There's no rule that says you need to be iterating over metals in order to call metals.get.

CodePudding user response:

There are 3 items in metals.items(). This is why the loop runs 3x. Just remove that statement; you don't need that loop

for unit in units:
    if unit == gold:
        ...
  • Related