250 likes | 270 Views
MIS 4395.A Structured Programming Language. Ahmed Imran Kabir Week 9, Dictionaries. Characteristics of Dictionaries. Dictionaries and lists share the following characteristics: Both are mutable. Both are dynamic. They can grow and shrink as needed.
E N D
MIS 4395.AStructured Programming Language Ahmed Imran Kabir Week 9, Dictionaries
Characteristics of Dictionaries • Dictionaries and lists share the following characteristics: • Both are mutable. • Both are dynamic. They can grow and shrink as needed. • Both can be nested. A list can contain another list. A dictionary can contain another dictionary. A dictionary can also contain a list, and vice versa. • Dictionaries differ from lists primarily in how elements are accessed: • List elements are accessed by their position in the list, via indexing. • Dictionary elements are accessed via keys.
Defining Dictionaries • Dictionaries are Python’s implementation of a data structure that is more generally known as an associative array. A dictionary consists of a collection of key-value pairs. Each key-value pair maps the key to its associated value. • You can define a dictionary by enclosing a comma-separated list of key-value pairs in curly braces ({}). A colon (:) separates each key from its associated value:
Defining Dictionaries (Cont.) • Retrieving the values in the sublist or subdictionary requires an additional index or key: This example exhibits another feature of dictionaries: the values contained in the dictionary don’t need to be the same type. In person, some of the values are strings, one is an integer, one is a list, and one is another dictionary.
Defining Dictionaries (Cont.) • Just as the values in a dictionary don’t need to be of the same type, the keys don’t either: Here, one of the keys is an integer, one is a float, and one is a Boolean. It’s not obvious how this would be useful, but you never know. Notice how versatile Python dictionaries are. In MLB_team, the same piece of information (the baseball team name) is kept for each of several different geographical locations. person, on the other hand, stores varying types of data for a single person. You can use dictionaries for a wide range of purposes because there are so few limitations on the keys and values that are allowed. But there are some. Read on!
Restrictions on Dictionary Keys • Almost any type of value can be used as a dictionary key in Python. You just saw this example, where integer, float, and Boolean objects are used as keys:
Restrictions on Dictionary Keys (Cont.) • You can even use built-in objects like types and functions:
Restrictions on Dictionary Keys (Cont.) • However, there are a couple restrictions that dictionary keys must abide by. First, a given key can appear in a dictionary only once. Duplicate keys are not allowed. A dictionary maps each key to a corresponding value, so it doesn’t make sense to map a particular key more than once. You saw above that when you assign a value to an already existing dictionary key, it does not add the key a second time, but replaces the existing value:
Restrictions on Dictionary Keys (Cont.) • Similarly, if you specify a key a second time during the initial creation of a dictionary, the second occurrence will override the first:
Restrictions on Dictionary Keys (Cont.) • Secondly, a dictionary key must be of a type that is immutable. You have already seen examples where several of the immutable types you are familiar with—integer, float, string, and Boolean—have served as dictionary keys. A tuple can also be a dictionary key, because tuples are immutable:
Restrictions on Dictionary Keys (Cont.) • Recall from the discussion on tuples that one rationale for using a tuple instead of a list is that there are circumstances where an immutable type is required. This is one of them.) However, neither a list nor another dictionary can serve as a dictionary key, because lists and dictionaries are mutable:
Restrictions on Dictionary Values • By contrast, there are no restrictions on dictionary values. Literally none at all. A dictionary value can be any type of object Python supports, including mutable types like lists and dictionaries, and user-defined objects, which you will learn about in upcoming tutorials. There is also no restriction against a particular value appearing in a dictionary multiple times:
Conclusion Lists and dictionaries are two of the most frequently used Python types. As you have seen, they have several similarities, but differ in how their elements are accessed. Lists elements are accessed by numerical index based on order, and dictionary elements are accessed by key Because of this difference, lists and dictionaries tend to be appropriate for different circumstances. You should now have a good feel for which, if either, would be best for a given situation.