I've run into an issue that I've most likely solved before but forgot how I did.
I want to have a dict
be able to take as many values as you throw at it, in my case, 4 values, and not have a predetermined limit of values it expects to unpack.
The example input I'm trying to run is:
2
USA Boston Pittsburgh Washington Seattle
UK London Edinburgh Cardiff Belfast
This is a small debug build of the program. You input an int
and you input that many keys and their respective one value:
cNamesandCountries = dict()
n = int(input())
for i in range(n):
user_input = input()
key, value = user_input.split()
cNamesandCountries[key] = value
print(cNamesandCountries)
I attempted to fix the problem by doing this:
cNamesandCountries = dict()
n = int(input())
for i in range(n):
user_input = input()
key, value, value, value, value = user_input.split()
cNamesandCountries[key] = value, value, value, value
print(cNamesandCountries)
but the output I received was this:
{'USA': ('Seattle', 'Seattle', 'Seattle', 'Seattle'), 'UK': ('Belfast', 'Belfast', 'Belfast', 'Belfast')}
Of course, that isn't supposed to be happening. It's taking the last value of my input and using it for every value in my dict
. I want the output to be this:
{'USA': ('Boston', 'Pittsburgh', 'Washington', 'Seattle'), 'UK': ('London', 'Edinburgh', 'Cardiff', 'Belfast')}
I know that I can solve this issue by having the dict take in however many values are inputted into the code, but I don't know how I would get it to do that.