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 we entered them. We can index ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access