Show Menu
Cheatography

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?
len(m­yLi­stO­rTu­ple)
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?
myLis­tOr­Tup­le.c­ou­nt(­"­x")
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
 

Sets

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
mySet.ad­d("x­")
Remove element "­x" from a set
Method 1: mySet.re­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
mySet.cl­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
len(m­ySet)
 

Dictio­naries

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
myDic­t.k­eys()
Get the size of the dictionary
len(m­yDict)
Delete element with key "­k" from the dictionary
del myDict­["k"]
Delete all the elements in the dictionary
myDic­t.c­lear()
                   

Help Us Go Positive!

We offset our carbon usage with Ecologi. Click the link below to help us!

We offset our carbon footprint via Ecologi
 

Comments

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