Python Dictionary with multiple values
In Python, we cannot construct a dictionary with multiple values. However, we can construct a dictionary with multiples values by using tuple/list.
Here is the example:
|
1 2 3 4 5 6 7 8 |
personDatabase = {"personOne": (6,75), "personTwo": (6,80), "personThree": (7,85)} for eachPerson in personDatabase: print "Name: %s\t" % eachPerson print "-----------------" print "Height: %d\t Weight: %d\n" % personDatabase[eachPerson] |
Output:
Name: personTwo
———————-
Height: 6 Weight: 80
Name: personOne
———————-
Height: 6 Weight: 75
Name: personThree
———————-
Height: 7 Weight: 85
Explanation:
I constructed a personDatabase dictionary with a key and multiple values in a tuple. There are two values I used here which I refer to height and weight while printing the output.