Show Menu

Python3 data structures Cheat Sheet by

Lists and Tuples

What are lists and tuples?
Ordered 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?
myList­OrT­uple[x] -- if not found, throws IndexError
Is element "­x" in list/t­uple?
"­x" in myList­OrTuple
Index of element "­X" of list/t­uple?
myList­OrT­upl­e.i­nde­x("x­") -- If not found, throws a ValueError 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:­mov­e("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 TypeError exception
Insert element in position x of a list/t­uple?
Lists:­sert(x, "­val­ue")
Tuples: tuples are immutable!
Append "­x" to a list/t­uple?
Lists: myList.ap­pen­d("x­")
Tuples: tuples are immutable!
Convert a list/tuple to tuple/list
List to Tuple: tuple(­myList)
Tuple to List: list(m­yTuple)
Slicing list/tuple
myList­OrT­upl­e[i­nd1­:in­d2:­step] -- step is optional and may be negative


What is a set?
Unordered collection with no duplicate 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
Remove element "­x" from a set
Method 1: mySet.r­em­ove­("x") -- If "­x" is not present, raises a KeyErorr
Method 2: mySet.d­is­car­d("x­") -- Removes the element, if present
Remove every element from the set
Check if "­x" is in the set
"­x" in mySet
Union of two sets
Method 1: mySet1.un­ion­(my­Set2)
Method 2: mySet1 | mySet2
Inters­ection of two sets
Method 1:­ter­sec­t(m­ySet2)
Method 2: mySet1 & mySet2
Difference of two sets
Method 1: mySet1.di­ffe­ren­ce(­mySet2)
Method 2: mySet1 - mySet2
Simmetric difference of two sets
Method 1:­mme­tri­c_d­iff­ere­nce­(my­Set2)
Method 2: mySet1 ^ mySet2
Size of the set


What is a dictio­nary?
Unordered 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
myDict­["k"] = value
Update the element with key "­k"
myDict­["k"] = newValue
Get element with key "­k"
myDict­["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­ear()


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