Download presentation
Presentation is loading. Please wait.
Published byGodwin McBride Modified over 9 years ago
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…]
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.