Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to iterate through a list and a dictionary in a nested for loop

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

# Dictionary of metals and email addresses
metals = {
    'gold':'1@gmail.com',
    'silver':'2@gmail.com',
    'platinum':'3@gmail.com',
}

# 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 1@gmail.com, silver to 2@gmail.com, 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:

1@gmail.com
1@gmail.com
1@gmail.com
2@gmail.com
2@gmail.com
2@gmail.com
3@gmail.com
3@gmail.com
3@gmail.com
Keys: gold
Keys: silver
Keys: platinum
Values: 1@gmail.com
Values: 2@gmail.com
Values: 3@gmail.com
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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading