lang/python/ PythonNativeDataStructures
By 'native' I include things from the standard library like defaultdict
.
Sets
a = set()
a.add(1)
a.update(2,3,4)
b = set((2,3))
c = a.difference(b)
d = set((5,6))
e = c.union(d)
f = b.intersection(d)
Dicts
Comprehension
From keys, values as lists if key is numeric
keys = [ 1, 2, "hello", 4 ]
values = [ "hello", "world","mr", "flibble" ]
d = { k : v for k,v in zip(keys,values) if isinstance(k,int) }
rather than map and filter
a = [1,2,3,4,5,6,7]
f = lambda t: t**2-10
b = [x for x in a if x % 2 == 0] # instead of list(filter(lambda t: t % 2 == 0,a))
c = [f(x) for x in a] # instead of list(map(f,a))
d = [f(x) for x in a if x % 2 == 0] # instead of list(map(f,filter(lambda t: t%2==0,a)))
e = [f(x) for x in a if f(x) > 0] # instead of list(map(f,filter(lambda t: f(t)>0,a)))