Download presentation
Presentation is loading. Please wait.
1
Python Data Structures
CS 265 Seth Simpson
2
Lists Creating Lists List methods exampleList = [1,3,5,7,9]
Append(element) – appends element to end Extend(list) – extends the list with another list Insert(position, element) – insert element at the given position Remove(element) – removes the first occurrence of the element
3
Lists (Cont) More List methods
Pop(index) – Remove and return the element at index (last element by default) Index(element) – returns index of the first occurrence of element Count(element) – returns the number of times element appears in the list Sort() – sorts the list Reverse() – reverses the list
4
Example of List Methods
5
Lists as Stacks Last in – First out
List pop function enables us to treat lists as stacks.
6
Lists as Queues First in – first out
Popping end elements of a list is fast because the elements don’t need to be rearranged Popping from the front of a list is slow because the list needs to be rearranged
7
List Filtering Filter(function name, list)
Returns a sequence of items for which function(item) is true. Example:
8
List Mapping Map(function name, list)
Calls the function for each value in the list and outputs the results as list Example:
9
List Reducing Reduce(function name, list)
Returns a single value by computing the function value on the first two elements, then the result of that with the next element and so on.
10
Sets Unordered collection with no duplicates
11
Sets (Cont)
12
Dictionaries Unsorted set of Key : Value pairs
13
Dictionaries (Cont) Can also call the dict() constructor to create a dictionary. Printing out Dictionaries
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.