Indexing Data Using Dictionaries
A dictionary is a special kind of sequence that uses a name and value pair. The use
of a name makes it easy to access particular values with something other than a
numeric index. To create a dictionary, you enclose name and value pairs in curly
brackets. Create a test dictionary by typing MyDict
= {'Orange':1, 'Blue':2,
'Pink':3} and pressing Enter.
To access a particular value, you use the name as an index. For example, type
MyDict[‘Pink’] and press Enter to see the output value of
3
. The use of dictionar-
ies as data structures makes it easy to access incredibly complex data sets using
terms that everyone can understand. In many other respects, working with a
dictionary is the same as working with any other sequence.
Dictionaries do have some special features. For example, type MyDict.keys() and
press Enter to see a list of the keys. You can use the
values()
function to see the
list of values in the dictionary.
CHAPTER 5
Do'stlaringiz bilan baham: |