FallingCave
FallingCave

Reputation: 1

How can I have multiple different values to iterate through in the same for statement?

Error returns ValueError: too many values to unpack (expected 2)

accounts = [["Tom", "Boyle", "23"]]
for meta in "First Name", "Last Name", "Age":
    for account in accounts:
        # The line below is what I have trouble with
        for info, meta in account, ("First Name", "Last Name", "Age"): 
            print(meta + ": " + info)
            print("------------------------------------------")

            
""" Expected Output:
First Name: Tom
Last Lame: Boyle
Age: 23
------------------------------------------
"""

Upvotes: 0

Views: 38

Answers (1)

Tranbi
Tranbi

Reputation: 12711

I think what you're looking for is:

accounts = [["Tom", "Boyle", "23"]]
meta_list = ["First Name", "Last Name", "Age"]
for account in accounts:
    for meta, info in zip(meta_list,account): 
        print(meta + ": " + info)
    print("------------------------------------------")

Upvotes: 1

Related Questions