Download presentation
Presentation is loading. Please wait.
Published byIrma Booth Modified over 9 years ago
1
Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming
2
Announcements Exam 3: Exam 3: –Wednesday, May 1st Assignment 9 will be our last project (and there was much rejoicing!) Assignment 9 will be our last project (and there was much rejoicing!)
3
Other Data Structures Tuples Tuples Sets Sets Dictionaries Dictionaries
4
Tuples Sequential data structures Sequential data structures –Order matters –Lists and strings are also sequential Very similar to lists BUT immutable Very similar to lists BUT immutable –Can prevent data from being changed Uses: employee or student records Uses: employee or student records
5
Tuple Representation Like lists, but with parentheses instead of square brackets myTuple = (1,2,3) myTuple = (“a”, “b”, 3) myTuple = (1,2,1,2,3)
6
Tuple Operations OperationDescription []Indexing [:]Slicing +Concatenation *Repetition len()Length for elem in myTuple OR for i in range(len(myTuple)) then access with [] Traverse elem in myTupleMembership
7
Packing and Unpacking Tuples You can also create a tuple by packing values together: You can also create a tuple by packing values together: myTuple = “a”,4,”hello” And you can unpack them: And you can unpack them: letter, num, word = myTuple
8
Converting Tuples Convert from tuple to list using list Convert from tuple to list using listlist(myTuple) Convert from list to tuple using tuple Convert from list to tuple using tupletuple(myList)
9
Sets A set is an unordered collection with no duplicates A set is an unordered collection with no duplicates –Similar to mathematical sets
10
Set Representation Create from a list: Create from a list: mySet = set([1,2,3,4]) myList = [1,2,3,4,5] mySet = set(myList) Duplicate elements will be eliminated and the ordering lost Duplicate elements will be eliminated and the ordering lost
11
Set Operations Union ( | ) Union ( | ) –Elements in one or both sets Intersection ( & ) Intersection ( & ) –Elements in both sets Difference ( - ) Difference ( - ) Symmetrical Difference ( ^ ) Symmetrical Difference ( ^ ) –Elements in one but not both sets
12
More Set Operations OperationDescription.add( )Add an element to the set.clear()Remove all elements from the set len()Length for elem in myTupleTraverse elem in myTupleMembership
13
Question: What do the following datatypes have in common? List, String, Tuple A. All are immutable B. All are mutable C. All are sequential D. All are unordered
14
Dictionaries Unordered collection of key-value pairs Unordered collection of key-value pairs –Keys are used to find values in the collection –Called hash tables or maps in other languages Key must be an immutable type Key must be an immutable type Keys are unique in the dictionary Keys are unique in the dictionary
15
Dictionary Representation To represent a dictionary: To represent a dictionary:{<key>:<value>,<key>:<value>}Examples: myDictionary = {} #empty myDictionary = {“hammer”:”tool”, “ham”:”meat”}
16
Dictionary Operations Add to a dictionary by indexing with a key and assigning a value: Add to a dictionary by indexing with a key and assigning a value:myDictionary[<key>]=<value>Example: myDictionary = {} #empty myDictionary[“apple”]=“fruit” #{“apple”:”fruit”}
17
Dictionary Operations Change a value associated with a key by simply re-doing the assignment: Change a value associated with a key by simply re-doing the assignment:myDictionary[<key>]=<new_value>Example: myDictionary[“apple”]=“veggie” #{“apple”:”veggie”} myDictionary[“apple”]=“fruit” #{“apple”:”fruit”}
18
Dictionary Operations Remove from a dictionary by indexing with a key and using del: Remove from a dictionary by indexing with a key and using del: del myDictionary[ ] Example: myDictionary = {“apple”:”fruit”} del myDictionary[“apple”] #empty
19
Dictionary Operations Membership: Membership: in in Traverse: Traverse: for in for in List of Keys: List of Keys:<dictionary>.keys() List of Values: List of Values:<dictionary>.values()
20
More Dictionary Operations OperationDescription myDictionary.has_key( )Returns True if the dictionary contains the specified key and False otherwise myDictionary.items()Returns a list of tuples, where each tuple contains a key and value pair myDictionary.get(, )Returns value associated with key if it exists, otherwise returns default value myDictionary.clear()Removes all pairs from the dictionary
21
Question: For a value to act as a key in a dictionary, it must be: For a value to act as a key in a dictionary, it must be: A. A literal value B. A string C. Immutable D. All of the above
22
So… What types can be keys in a dictionary? What types can be keys in a dictionary? –Strings –Tuples –Numbers(!)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.