Collections#

This is a cheatsheet of Python’s collection classes.

Summary Comparison#

Action

List

Tuple

Set

Dictionary

Create

a = [ ]
a = list()
a = [1, 2]

t = () # empty
t = tuple()
t = (1, 2)

s = set( )
s = {1, 2}

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

Remove all

my_list.clear()
my_list = []
del my_list[:]

indexOf

my_list.index('value')
my_list.index('value', startIndex)
my_list.index('value', startIndex, endIndex)

Sort

my_list.sort() # inplace sorting
my_list = sorted(my_list) # reassignment

Reverse

rev_list = my_list.reverse()
rev_list = my_list[::-1]
rev_list = list(reversed(my_list)) # reversed returns an iterator
sorted_rev_list = sorted(my_list, reverse=True)

Copy

cloned_list = list.copy() # recall reference semantics

Tuple#

Operation

API

Create

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

Remove all

t = tuple() # construct an empty tuple

indexOf

t.index('value')
t.index('value', startIndex)
t.index('value', startIndex, endIndex)

Sort

t = tuple(sorted(t)) # sorted returns a list, recreate tuple

Reverse

rev_t = t[::-1] # fastest
rev_t = tuple(reversed(t)) # reversed returns an iterator
sorted_rev_t = tuple(sorted(t, reverse=True))

Copy

cloned_t = tuple(t) # recall reference semantics
cloned_t = (*t, ) # copy via unpacking

Set#

Operation

API

Create

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

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

Copy

clone_set = my_set.copy() # recall reference semantics

Get

NA, can only determine if item is in set or not

Set

NA, cannot set the value of a specific element

Insert

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 = [ n for n in range(100) ]

# set comprehension
my_set = { n for n in range(100) }

# dictionary comprehensions & construction
my_dict = { key: value for key, value in zip(range(10), range(10, 20)) } 
my_dict = dict( zip([k for k in range(10)], [v for v in range(10,20)]))
values = [ 6, 4, 2, 1, 3, 5]
my_dict = { key: values[key] for key in range(len(values)) }
my_dict = { key: value for key, value in enumerate(values) }

# There is NO tuple comprehension