d = {} d = dict() d = {k1:v1, k2:v2} d = dict(dict2)
Add / Insert
a.append(obj) a.insert(idx, obj)
t = t + (obj,)
s.add(3)
d['b'] = 2
Contains
if obj in a
if obj in t
if obj in s
if key in d
Remove
a.remove(obj) a.pop(index) a.pop()
NA
s.remove(obj) s.pop() s.discard('value')
d.pop(key) d.pop(key, def_val) del d[key] d.popitem()
Get / Set
v = a[index]
v = t[index] i1, i2 = t i1, *mid, i2 = t i1, *last = t
NA
v = d['a'] v = d.get('a', v)
List
Operation
API
Create
my_list = [] my_list = [x ** 2 for x in [1, 3, 5, 7]] # comprehension my_list = list() # constructor my_list = list(iterable) # construct list from any iterable
Add
my_list.append(5) my_list.append(list2) # my_list has list2 as the last item
Remove
my_list.remove('value') # inplace, no return value last = my_list.pop() # return and inplace remove last item in the list value = my_list.pop(index) del list[index] # inplace, no return value
Contains
if 'value' in my_list: # in operator
Get
n = my_list[index]
Set
my_list[index] = value
Size
len(my_list) # len is built-in function
Insert
my_list.insert(index, value)
Extend
my_list.extend(list2) # add all items in list2, not list2 as an item my_list += [5, 4, 3] # extends the list with items in the other list # don’t confuse append with extend
t = tuple() t = (1, ) # need the comma to differentiate from an integer t = (1, 2) # constructor t = tuple(iterable) # construct tuple from any iterable
Add
t = t + (3, ) # tuples are immutable! t += (3, ) # same as above with shorthand operator t = (*t, 3) # same as above, but uses unpacking
Remove
cannot remove since tuples are immutable t = t[1:] # use slicing to create new tuple
Contains
if 'value' in t: # in operator
Get
n = t[index]
Set
NA # cannot set since tuples are immutable
Size
len(t) # len is built-in function
Insert
cannot insert since tuples are immutable # we can concatenate with the + operator t = t[index] + (3, ) + t[index:]
Extend
+ operator will concatenate which is extend t += t2 # concatenate add all items in t2 t += (*t2, ) # same as above but using unpacking
my_set = set() # constructor my_set = set(iteratable) # create set from any iterable my_set = { x**2 for x in range(11) } # comprehension # Note: empty {} creates a dictionary not a set
Add
my_set.add(5) my_set.update({1, 2, 3}) # inplace union of the sets
Remove
my_set.remove('value') # raise error if not found my_set.discard('value') # no error raised if not found my_set.pop() # remove a RANDOM item from the set my_set.difference_update(set2) # inplace removes set2 items diff_set = my_set - remove_set # difference operator
Contains
if value in my_set: # in operator
Size
len(my_set) # len is built-in function
Remove All
my_set.clear() my_set = set()
Union
my_set = set1.union(set2) # union does not change the set my_set = s1 | s2 # same as above
Intersection
my_set = s1.intersection(s2) # does not change the set my_set = s1 & s2 # same as above
Difference
my_set = s1.difference(s2) # difference does not change the set my_set = s1 - s2 # same as above
NA Just add the object to the set. There is no order of elements.
indexOf / Sort / Reverse
NA There is no order in a set
Dictionary
Operation
API
Create
d = dict() d = {} # empty dictionary d = {k1:v1} # initialize a set with 1 key/value pair
Add
d[key] = value # adds or sets the value d.update(dict2) # inplace addition of all items in dict2 d = { **d1, **d2 } # creates with all unpacked items of d1 & d2
Remove
last = d.popitem() # return & remove the last item inserted item = d.pop(key) # return value & remove item at key del d[key] # remove item at key, returns None
Contains
if key in d: # in operator. Finds key only
Get
value = d[key] # raises error if no key value = d.get(key, def_value) # avoids error if not found
Set
d[key] = value # adds or sets the value d.setdefault(key, def_value) # sets only if key not in d. returns d[key]
Size
len(d) # len is built-in function
Insert
NA # dictionary order cannot change
Extend
d.update(dict2) # inplace addition of all items in dict2
Remove All
d.clear() d = dict()
indexOf
No easy way. Must iterate through values key = [k for k, v in d.items() if v == target][0] # error if not present
Sort
python retains original order of dictionary. recreate dictionary sorted_d = { key:d[key] for key in sorted(d.keys()) }
Reverse
rev_dict = { k:d[k] for k in reversed(d.keys())}
Copy
clone_dict = d.copy()
Misc
my_dict.keys() my_dict.values() my_dict.items()
Simple Comprehensions
# list comprehension
my_list=[nforninrange(100)]# set comprehension
my_set={nforninrange(100)}# dictionary comprehensions & construction
my_dict={key:valueforkey,valueinzip(range(10),range(10,20))}my_dict=dict(zip([kforkinrange(10)],[vforvinrange(10,20)]))values=[6,4,2,1,3,5]my_dict={key:values[key]forkeyinrange(len(values))}my_dict={key:valueforkey,valueinenumerate(values)}# There is NO tuple comprehension