Dictionaries
Unlike lists, dictionaries are collections with no fixed order. Instead, they have a key (the name of the element) and a value (the content of the element), and Python places them wherever it needs to for maximum performance. When defining dictionaries, you need to use braces ({ }
) and colons (:
). You start with an opening brace and then give each element a key and a value, separated by a colon, like this:
>>> mydict = { "perl" : "a language", "php" : "another language" }>>> mydict{'php': 'another language', 'perl': 'a language'}
This example has two elements, with keys perl
and php
. However, when the dictionary is printed, we find that php
comes before perl
—Python hasn’t respected the order in which ...
Get Ubuntu Unleashed 2014 Edition: Covering 13.10 and 14.04,Ninth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.