Python Conditionals, Lists, Dictionaries, and Operators - Dictionaries (Page 5 of 6 )
Another way to store multiple values is with a dictionary. The difference between a List and a dictionary is that dictionaries are unordered and do not use indexes. The best way to understand them is to see them at work:
#!/usr/local/bin/python
mydiction = {'FirstName':'John', 'LastName':'Cleese'}
print mydiction[FirstName]
The above code will result in the printout:
John
This is because the value to the left of the colon (:) acts as the index, while the value to the right of the colon acts as the value being referenced.
You can also add two dictionaries together:
#!/usr/local/bin/python
mydiction = {'FirstName':'John', 'LastName':'Cleese'}
abc = {'FirstName': 'Monty', 'LastName':'Python')
mydiction.update(abc)
print mydiction
This will combine the two dictionaries and result in the printout:
John Cleese Monty Python
Next: Operators >>
More Python Articles
More By James Payne