Write code.
— 9 min read

Python List

Using Lists in Python.

list.append(element)

1 #appen an element to a list
2 li = [1, 2, 3, 4, 5]
3 li.append(6)
4 print li # [1, 2, 3, 4, 5, 6]

Sets

 1 a.issubset(b)
 2 #<==>
 3 a & b == a
 4 
 5 a.union(b)
 6 #<==>
 7 a | b
 8 
 9 a.intersect(b)
10 #<==>
11 a & b
12 
13 a.difference(b)
14 # <==>
15 a - b
16 
17 a.symmetric_difference(b)
18 #<==>
19 a ^ b
20 
21 #set ==> list
22 list(ensemble)
23 
24 s1 = set([1, 2, 3, 4, 5, 6])
25 s2 = set([2, 3, 4])
26 s3 = set([6, 7, 8, 9])
27 
28 print s1 & s2 # set([2, 3, 4])
29 print s1 | s3 # set([1, 2, 3, 4, 5, 6, 7, 8, 9])
30 print s1 & s3 # set([6])
31 print s1 ^ s3 # set([1, 2, 3, 4, 5, 7, 8, 9])
32 print s1 - s2 # set([1, 5, 6])

list.extend(other_list)

1 #extend list and append to it element from other_list
2 li = [1, 2, 3, 4, 5]
3 li.extend([6, 7, 8]) # Same as li += [6, 7, 8]
4 print li # [1, 2, 3, 4, 5, 6, 7, 8]

list.remove(other_list)

1 #remove the first value equal to v from list
2 li = [1, 2, 3, 4, 5]
3 li.remove(1)
4 print li # [2, 3, 4, 5]

list.reverse()

1 #reverse the list
2 li = [1, 2, 3, 4, 5]
3 li.reverse()
4 print li # [5, 4, 3, 2, 1]
5 
6 print reversed(li) # [1, 2, 3, 4, 5]
7 print li # [5, 4, 3, 2, 1] <-- not modified

list.sort()

1 #sort list
2 li = [3, 1, 4, 2, 5]
3 li.sort()
4 print li # [1, 2, 3, 4, 5]
5 
6 li = [3, 1, 4, 2, 5]
7 print sorted(li) # [1, 2, 3, 4, 5]
8 print li # [3, 1, 4, 2, 5] <-- not modified

map(callback, list)

 1 #use of callbacks in map
 2 # <==>
 3 # [a, b, c] -> [callback(a), callback(b), callback(c)]
 4 def map(callback, l):
 5     new_l = []
 6     for element in l:
 7        new_l.append(callback(element))
 8     return new_l
 9 
10 def sqr(x): return x ** 2
11 def pair(x): return not bool(x % 2)
12 
13 print map(sqr [1, 2, 3, 4, 5]) # [1, 4, 9, 16, 25]
14 
15 print map(pair, [1, 2, 3, 4, 5]) # [False, True, False, True, False]

filter(callback, list)

 1 #useof callback in filter
 2 # <==>
 3 # [a, b, c] -> [a if callback(a) == True, b if callback(b) == True, c if callback(c) == True]
 4 def filter(callback, l):
 5     new_l = []
 6     for element in l:
 7        if callback(element): new_l.append(element)
 8     return new_l
 9 
10 #e.g
11 def sqr_less_16(x): return x ** 2 < 16
12 def pair(x): return not bool(x % 2)
13 
14 print filter(sqr_less_16, [1, 2, 3, 4, 5]) # [1, 2, 3]
15 
16 print filter(pair, [1, 2, 3, 4, 5]) # [2, 4]
17 
18 
19 # pair element
20 print [x for x in l if x % 2 == 0] # <==> filter + easy to use.
21 
22 # use of both pair sqr
23 print [x ** 2 for x in l if x ** 2 % 2 == 0]
24 # <==>
25 print [x for x in [a ** 2 for a in l] if x % 2 == 0]

reduce(callback, list, init_value)

 1 #use of callback in reduce
 2 # <==>
 3 # [a, b, c] -> callback(callback(a, b), c)
 4 def reduce(callback, l, initial_value):
 5     if l == []: return initial_value
 6     else: return callback(l[0], reduce(callback, l[1:], initial_value))
 7 
 8 #e.g
 9 def add(x, y): return x + y
10 def prod(x, y): return x * y
11 
12 print reduce(add, [1, 2, 3], 0) # ((0 + 1) + 2) + 3
13 print reduce(prod, [1, 2, 3], 1) # ((1 * 1) * 2) * 3

list <==> string

1 print ", ".join(["a", "b", "c", "d"]) # print list element + ", " in between, <==> "a, b, c, d".
2 print "a b c d".split(" ") # <==> [a, b, c, d]
3 
4 l = [
5     (105, "d"),
6     (21, "z"),
7     (0, "v")
8 ]
9 l.sort() # sort by first element then second