Show Menu

Python3 data structures Cheat Sheet by

Lists and Tuples

What are lists and tuples?
Orde­red sequence of values indexed by integer numbers. Tuples are immutable.
How to initialize an empty list/t­uple?
Lists: myList = []
Tuples: myTuple = ()
Size of list/t­uple?
Get element in position x of list/t­uple?
myLis­tOr­Tup­le[x] -- if not found, throws Index­Error
Is element "­x" in list/t­uple?
"x" in myList­OrT­uple
Index of element "­X" of list/t­uple?
myLis­tOr­Tup­le.i­nd­ex(­"­x") -- If not found, throws a Value­Error exception
Number of occurr­ences of "­x" in list/t­uple?
Update an item of a list/t­uple?
Lists: myList[x] = "x"
Tuples: tuples are immutable!
Remove element in position x of list/t­uple?
Lists: del myList[x]
Tuples: tuples are immutable!
Remove element "­x" of a list/t­uple?
Lists: myLis­t.r­emo­ve(­"­x"). Removes the first occurrence
Tuples: tuples are immutable!
Concat­enate two lists or two tuples?
Lists: myList1 + myList2
Tuples: myTuple1 + myTuple2
Concatenating a List and a Tuple will produce a TypeE­rror exception
Insert element in position x of a list/t­uple?
Lists: myLis­t.i­nse­rt(x, "value")
Tuples: tuples are immutable!
Append "­x" to a list/t­uple?
Lists: myList.append("x")
Tuples: tuples are immutable!
Convert a list/tuple to tuple/list
List to Tuple: tuple(myList)
Tuple to List: list(­myT­uple)
Slicing list/tuple
myLis­tOr­Tup­le[­ind­1:i­nd2­:step] -- step is optional and may be negative


What is a set?
Unor­dered collection with no duplic­ate elements. Sets support mathem­atical operations like union, inters­ection, difference and simmetric differ­ence.
Initialize an empty set
mySet = set()
Initialize a not empty set
mySet = set(el­ement1, elemen­t2...) -- Note: strings are split into their chars (dupli­cates are deleted). To add strings, initialize with a Tuple/List
Add element "­x" to the set­d("x­")
Remove element "­x" from a set
Method 1:­mov­e("x­") -- If "­x" is not present, raises a KeyErorr
Method 2: mySet.di­sca­rd(­"­x") -- Removes the element, if present
Remove every element from the set­ear()
Check if "­x" is in the set
"x" in mySet
Union of two sets
Method 1: mySet1.union(mySet2)
Method 2: mySet1 | mySet2
Inters­ection of two sets
Method 1: mySet1.intersect(mySet2)
Method 2: mySet1 & mySet2
Difference of two sets
Method 1: mySet1.difference(mySet2)
Method 2: mySet1 - mySet2
Simmetric difference of two sets
Method 1: mySet1.symmetric_difference(mySet2)
Method 2: mySet1 ^ mySet2
Size of the set


What is a dictio­nary?
Unor­dered set of key:value pairs . Members are indexed by keys (immutable objects)
Initialize an empty Dict
myDict = {}
Add an element with key "­k" to the Dict
myDic­t["k­"] = value
Update the element with key "­k"
myDic­t["k­"] = newValue
Get element with key "­k"
myDic­t["k­"] -- If the key is not present, a KeyError is raised
Check if the dictionary has key "­k"
"k" in myDict
Get the list of keys
Get the size of the dictionary
Delete element with key "­k" from the dictionary
del myDict­["k"]
Delete all the elements in the dictionary


No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

            Python 3 Cheat Sheet by Finxter