Python3 data structures Cheat Sheet
by desmovalvo via cheatography.com/56139/cs/14893/
Lists and Tuples Lists and Tuples (cont) Sets (cont)
What are lists and tuples? Append "x" to a list/tuple? Intersection of two sets
Ordered sequence of values indexed by Lists: myList.append("x") Method 1: mySet1.intersect(mySet2)
integer numbers. Tuples are immutable. Tuples: tuples are immutable! Method 2: mySet1 & mySet2
How to initialize an empty list/tuple? Convert a list/tuple to tuple/list Difference of two sets
Lists: myList = [] List to Tuple: tuple(myList) Method 1: mySet1.difference(mySet2)
Tuples: myTuple = () Tuple to List: list(myTuple) Method 2: mySet1 - mySet2
Size of list/tuple? Slicing list/tuple Simmetric difference of two sets
len(myListOrTuple) myListOrTuple[ind1:ind2:step]-- Method 1:
step is optional and may be negative mySet1.symmetric_difference(mySet2)
Get element in position x of list/tuple?
Method 2: mySet1 ^ mySet2
myListOrTuple[x] -- if not found,
Sets
throws IndexError Size of the set
What is a set? len(mySet)
Is element "x" in list/tuple?
Unordered collection with no duplicate
"x" in myListOrTuple
elements. Sets support mathematical
Dictionaries
Index of element "X" of list/tuple? operations like union, intersection,
difference and simmetric difference. What is a dictionary?
myListOrTuple.index("x") -- If not
Unordered set of key:value pairs . Members
found, throws a ValueError exception Initialize an empty set
are indexed by keys (immutable objects)
mySet = set()
Number of occurrences of "x" in list/tuple?
Initialize an empty Dict
myListOrTuple.count("x") Initialize a not empty set
myDict = {}
mySet = set(element1,
Update an item of a list/tuple?
element2...) -- Note: strings are split Add an element with key "k" to the Dict
Lists: myList[x] = "x"
into their chars (duplicates are deleted). To myDict["k"] = value
Tuples: tuples are immutable!
add strings, initialize with a Tuple/List
Update the element with key "k"
Remove element in position x of list/tuple?
Add element "x" to the set
myDict["k"] = newValue
Lists: del myList[x]
mySet.add("x")
Tuples: tuples are immutable! Get element with key "k"
Remove element "x" from a set
myDict["k"] -- If the key is not present, a
Remove element "x" of a list/tuple?
Method 1: mySet.remove("x") -- If KeyError is raised
Lists: myList.remove("x").
"x" is not present, raises a KeyErorr
Removes the first occurrence Check if the dictionary has key "k"
Method 2: mySet.discard("x") --
Tuples: tuples are immutable!
Removes the element, if present "k" in myDict
Concatenate two lists or two tuples?
Remove every element from the set Get the list of keys
Lists: myList1 + myList2
mySet.clear() myDict.keys()
Tuples: myTuple1 + myTuple2
Concatenating a List and a Tuple will Check if "x" is in the set Get the size of the dictionary
produce a TypeError exception "x" in mySet len(myDict)
Insert element in position x of a list/tuple? Union of two sets Delete element with key "k" from the dictionary
Lists: myList.insert(x, "value") Method 1: mySet1.union(mySet2) del myDict["k"]
Tuples: tuples are immutable! Method 2: mySet1 | mySet2
Delete all the elements in the dictionary
myDict.clear()
By desmovalvo Published 2nd March, 2018. Sponsored by Readability-Score.com
cheatography.com/desmovalvo/ Last updated 2nd March, 2018. Measure your website readability!
Page 1 of 1. https://readability-score.com