60 likes | 188 Views
Dictionaries. Dictionaries. Allow you to retrieve data using a key (instead of an index). Classic dictionary: Search/retrieve using a word Get the definition of the word Python dictionary: Retrieve using a key Get data associated with the key. Creating a dictionary.
E N D
Dictionaries • Allow you to retrieve data using a key (instead of an index). • Classic dictionary: • Search/retrieve using a word • Get the definition of the word • Python dictionary: • Retrieve using a key • Get data associated with the key
Creating a dictionary • students = {‘Joe’:[CPSC231,MATH271],’Katie:[PSYC205,ENGL201]} • dict = {}
Adding and Retrieving Values • students[‘Bob’] = [CPSC231,PHIL279] • x = students[‘Joe’]
Keys • Type must be immutable • Strings • Integers • Tuples • etc • Keys in a dictionary must be unique
Methods • clear • get • items • keys • values