DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in...

28
DICTIONARIES

Transcript of DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in...

Page 1: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

DICTIONARIES

Page 2: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

The Compound Sequence Data Types

• All of the compound data types we have studies in detail so far– strings– lists– Tuples

• They are sequence type, which use integers as indices to access the values they contain within them. because their items occur in order.

Page 3: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

Compound data types

• Dictionaries are yet another kind of compound type. They are Python’s built-in mapping type.

• They maps key, which can by any immutable type, to values, which can be any type, just like the elements of a list or tuple.

• They are call associative arrays since they associate a key with a value.

Page 4: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

DICTIONARIES

• As a example, we will create a dictionary to translate English words into Spanish. For this dictionary, the keys are strings.

• One way to create a dictionary is to start with the empty dictionary and add key : value pairs. The empty is denoted {}:

Page 5: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

A dictionary to translate English word to spanish

The first assignment create a dictionary name s.The other assignments add new key-value pairs to the dictionary.

Page 6: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

A dictionary to translate English word to spanish

The key-value pairs of the dictionary are seperated by commas. Each pair contains a key and a valueseparated by a colon

Page 7: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

The Order of the Pairs

The order of the pairs may not be what you expected. Python uses complex algorithms, designed forvery fast access, to determine where the key-value pairs are stored in a dictionary. For our purposeswe can think of this ordering as unpredicatable.

Page 8: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

Another way to create a dictionary

Another way to create a dictionary is to provide a list of key-value pairs using the same syntax as the previous output:It doesn’t matter what order we write the pairs. The values in a dictionary are accessed with keys, not with indices, so there is no need to care about ordering.

Page 9: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

The Compound Non-Sequence Type

• The dictionary is the first compound type that we’ve seen that is not a sequence, so we can’t index or slice a dictionary.

Page 10: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

Dictionary operations

Page 11: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

del operation

The del statement removes a key-value pair from a dictionary. For example, the dictionary contains the names of various fruits and the number of each fruit in stock:If someone buys all of the oranges, we can remove the entry from the dictionary:

Page 12: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

Dictionary operations

Page 13: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

Dictionaries operation

A new shipment of bananas arriving could be handled like this:

Page 14: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

Dictionary methods• Dictionaries have a number of useful built-in

methods.• The keys method returns what Python 3 calls

view of its underlying keys.• A view object has some similarities to the

range object.

Page 15: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Page 16: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

Omit the keys method call in the for loop

Page 17: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

The values method returns a view object which can be turned into a list

Page 18: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

The items method returns a view, which promises a list of tuples - One tuple for each key: value pair:

Page 19: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

Tuples are often useful for getting both the key and the value at the same time while we are looping

Page 20: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

in and not in

Page 21: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

Looking up a non-existent key

Page 22: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

Alliasing and copying

• As in the case of lists, because dictionaries are mutable, you need to be aware of aliasing. Whenever two variables refer to the same object, changes to one affect the other.

Page 23: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

• If you want to modify a dictionary and keep a copy of the original, use the copy method.

• For example opposites is a dictionary that contains pairs of opposites:

Alliasing and copying

Page 24: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

alias and opposites refer to the same object;copy refers to a fresh copy of the same dictionary.

Page 25: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

Linked List

A linked list is either:1. the empty list, represented by None, or2. a node that contains a cargo object and a refe

rence to a linked list.

Page 26: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.

The Node class

Page 27: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Page 28: DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.