Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 231 Lab 8.

Similar presentations


Presentation on theme: "CSE 231 Lab 8."— Presentation transcript:

1 CSE 231 Lab 8

2 Dictionaires vs lists Creating Dictionaries Dictionaries – Methods
Topics to cover Dictionaires vs lists Creating Dictionaries Dictionaries – Methods

3 Dictionaries - Visualizing
Insertion-ordered collection of Python objects (Python >=3.6) What is a key? What is a value? Should be immutable Any objects is allowed A key is unique, there are no duplicates. Think of a word in a dictionary. There is only one entry for each word. You won’t find “key” in the dictionary twice.

4 Dictionaries vs Lists

5 Given a dictionary, D, where: D[“key”] = “value” print(D[“key”])
What happens? Given a dictionary, D, where: D[“key”] = “value” print(D[“key”]) “value” The above format will print the value associated with the key in the dictionary. The key is “key”, the value is “value”, so this will print “value”

6 Create and update a dictionary
Are dictionaries mutable? Creating a Dictionary M = {} M = dict() #preferred M = { 200: "EE", 100: "ME"} Adding/updating a value in a Dictionary M[500] = "CS"

7 Patterns Ints: Strings: Lists: x = 0 # initialize at zero
x += 1 # increment the counter Strings: s = ‘’ # initialize with empty string s += ch # concatenate characters Lists: L = [] # initialize with empty list L.append(value) # append values to the list

8 Patterns Dictionaries: D = { } # initialize with empty dictionary
S = “aabacdbacd” # we have a string, we want to count all the characters for ch in S: if ch in D: # check to see if the key exists in the dictionary D[ch] += 1 # increment the value if it exists else: D[ch] = 1 # set the value to 1 if it doesn’t exist

9 Dictionaries – Methods
.items() # Returns a set-like object of all the key-value pairs. .keys() # Returns a set-like object of all the keys. .values() # Returns a set-like pbject of all the values. .pop(key) # Removes entire associated with key and returns it’s value.

10 more Useful Methods & functions
D = M.get( 200 ) returns the value if key exists, else returns None E = M.pop( "Mike", None ) pops (and returns) the value if the key exists, returns the second argument otherwise del M[600] deletes if the key exist, KeyError if it doesn't D = dict(L) # assuming L = [('a',1),('b',2),('c',3)] D = {'a': 1, 'b': 2, 'c': 3}

11 Iteration for key in M.keys(): for key in M: for value in M.values():
for key, value in M.items():

12 D = {“one” : 1, “two” : 2, ”three” : 3} for x,y in D.items():
What happens? Given a dictionary D, D = {“one” : 1, “two” : 2, ”three” : 3} for x,y in D.items(): print(x,y) one 1 two 2 three 3 x and y will give each key and the corresponding value from the dictionary, so this statement will print every key-value pair in the dictionary

13 What happens? Given a dictionary D, D = {} Print(D[‘a’]) KeyError

14 Other useful methods and function
D = {“one” : 1, “two” : 2, ”three” : 3} list(D) # returns a list of all the keys used in the dictionary ['one', 'two', 'three'] sorted(D) # in insertion order (if you want it sorted, just use sorted instead) ['one', 'three', 'two'] Performing list(d) on a dictionary returns a list of all the keys used in the dictionary, in insertion order (if you want it sorted, just use sorted(d) instead). To check whether a single key is in the dictionary, use the in keyword.


Download ppt "CSE 231 Lab 8."

Similar presentations


Ads by Google