41,570 views 8,889 downloads 2 Mar 2018 History
3.8 avg · 4 ratings

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?
len(my­Lis­tOr­Tuple)
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?
myList­OrT­upl­e.c­oun­t("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: myList.re­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: myList.in­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

Sets

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
mySet.a­dd­("x")
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
mySet.c­lear()
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: mySet1.in­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: mySet1.sy­mme­tri­c_d­iff­ere­nce­(my­Set2)
Method 2: mySet1 ^ mySet2
Size of the set
len(mySet)

Dictio­naries

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
myDict.keys()
Get the size of the dictionary
len(my­Dict)
Delete element with key "­k" from the dictionary
del myDict­["k"]
Delete all the elements in the dictionary
myDict.cl­ear()

Badges

Useful Cheat Sheet Nice Cheat Sheet Good Cheat Sheet Downloaded from Unusual Locations × 5

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.