From the course: Python Data Analysis
Unlock the full course today
Join today to access over 24,500 courses taught by industry experts.
Dictionaries and sets - Python Tutorial
From the course: Python Data Analysis
Dictionaries and sets
- [Instructor] The other super important data structure in Python is the dictionary or dict. While lists and tuples give us a way to retrieve values by their numerical index, dictionaries associate values to unique keys. Dicts are written with curly braces separating items with commas. Each item is given as key colon value. For instance, here are the capitals of some of my favorite countries. The length of a dictionary is obtained with len, and the empty dictionary is denoted by empty braces. Just as we do with lists, values are accessed with a bracket notation, but instead of a number, we're going to use a key, which is usually a string. The same notation can be used to add items to a dictionary. Trying to access a non-existent item results in a key error. To avoid that, we can check beforehand whether an item exists in a dictionary using the in operator. Combined two dictionaries, we can unpack them within the same braces using the double star unpacking operator, which works in a…