Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value Error calling for more than 2 values

I'm getting this value error

ValueError: need more than 2 values to unpack)

and I don't know what it means.

Here is my code:

contact_map = {'Dennis Jones': ('989-123-4567', '[email protected]'), 'Susan': ('517-345-1234', '[email protected]'), 'Miller, Matthew': ('616-765-4321', '[email protected]')}
FORM = "{:<s};{:<d};{:<s}"

out_file = input("Enter a name for the output file: ")
output_file= open(out_file, "w")

for name, phone, email in contact_map.items():
    output_file.write(FORM.format(name, phone, email))

output_file.close()
like image 684
Drew Glapa Avatar asked Jan 18 '26 16:01

Drew Glapa


2 Answers

There must be two present when invoking dict.items() key and value. After that you need to unpack the value part in-order to get phone and email.

for name, value in contact_map.items():
    phone = value[0]
    email = value[1]
    output_file.write(FORM.format(name, phone, email))
like image 125
Avinash Raj Avatar answered Jan 21 '26 06:01

Avinash Raj


You're getting the error because you're attempting to unpack a tuple of len==3 (name, phone, email) but the items() returns (key, value), where value in this case is a tup of length 2.

You can unpack this in one line like:

for name, (phone, email) in contact_map.items():
like image 31
David Zemens Avatar answered Jan 21 '26 06:01

David Zemens