Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dictionaries Last half of Chapter 5. Dictionary A sequence of key-value pairs – Key is usually a string or integer – Value can be any python object There.

Similar presentations


Presentation on theme: "Dictionaries Last half of Chapter 5. Dictionary A sequence of key-value pairs – Key is usually a string or integer – Value can be any python object There."— Presentation transcript:

1 Dictionaries Last half of Chapter 5

2 Dictionary A sequence of key-value pairs – Key is usually a string or integer – Value can be any python object There is no order to the elements – So, there isn't a first or a last element – Python uses its own algorithm to order elements (for efficient access) You access values by using the key.

3 Dictionary operations Creating an empty dictionary: D = {} Creating a dictionary with string-float pairs D2 = {"ABC":42.9, "DEF":13.8, "GHI":-19.1} Getting a list of keys or values D2.keys() # Returns ["ABC", "GHI", "DEF"] or something like this. Getting a list of values D2.values() # Returns [42.9, -19.1, 13.8]

4 Dictionary Index'ing Using an existing element print(D2["ABC"]) Changing a value for an existing key D2["ABC"] = 100.3 Adding a new value D2["XYZ"] = 0.0 Note how this looks the same as modifying.

5 Dictionary examples [Using integers as a key] [Reiner Tile Sets…]


Download ppt "Dictionaries Last half of Chapter 5. Dictionary A sequence of key-value pairs – Key is usually a string or integer – Value can be any python object There."

Similar presentations


Ads by Google