Python Cheatsheet 1 – list, dict and set

This post is a cheatsheet on:

  1. list method
  2. dictionary method
  3. set method

List Method

Index Method Return Note
1 min(list) minimum element NOT method, function.
2 max(list) maximum element NOT method, function.
3 .append(object)
4 .extend(iterable) by iterating the iterable and appending each element
5 .insert(index, object) let object the take the index
6 .count(element) int If not exist, return 0
7 .index(element, [start, [stop]]) lowest index If not exist, raise ValueError
8 .pop(index=-1) element
9 .remove(element) remove the first occurrence of the element
10 .reverse()
11 .sort(key=None, reverse=False) IN PLACE, ascending order. When sorting a list of sequences,use kwarg key= to extract the key.

Dictionary Method

Index Method Return Notes
1 .pop(key[, default]) value / default
2 .popitem() (key, value)
3 .get(key) value / None
4 .copy() a shallow copy
5 .clear() reset the dictionary as empty
6 .update(dict2) merge dict1 and dict2, overwrite value of the same key
7 .keys() dict_keys object Series object
8 .values() dict_values object Series object.
9 .items() dict_items object Series object.
10 .zip() zip object (iterator) Iterator object

Series object:
1. iterable and can be transformed into list.
2. cannot be indexed.

Iterator object:
1. cannot directly print, must iterate or transform to series.
2. exhaustable, can be iterated only once.
3. ignore outstanding element, if any.

Set Method

Index Method Return Note
1 .add(object) cannot add mutable object or add to frozenset, have no effect if the object is already there
2 .clear()
3 .copy() a shallow copy
4 .difference(set) / – a new set delete elements in the front that occurs in the back, return the front
5 .difference_update(set) x.difference_update(y) is the same as “x = x – y”
6 .discard(element) if the element is not there, raise KeyError
7 .remove(element) if the element is not there, have no effect
8 .intersection(set) / & a new set
9 .isdisjoint() bool
10 .issubset() / <= / = / > bool

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

Up ↑

%d bloggers like this: