Python (5) built-in function

built-in function, none

If you want to check that an object is a list or a tuple, pass several classes to isinstance

if isinstance(e, (list, tuple)):

Flatten an array

>>> def list_flatten(l, a=None):
...     a = list(a) if isinstance(a, (list, tuple)) else []
...     for i in l:
...         if isinstance(i, (list, tuple)):
...             a = list_flatten(i, a)
...         else:
...             a.append(i)
...     return a
>>> l = [2, 1, [3, [4, [5]], 6], 7, [8]]
>>> print list_flatten(l, a=None)
[2, 1, 3, 4, 5, 6, 7, 8]

types.StringTypes: A sequence containing StringType and UnicodeType
types.StringType: A sequence containing StringType

import types
isinstance('abc', types.StringType)
#True
isinstance(u'abc', types.StringType)
#False
isinstance('abc', types.StringTypes)
#True
isinstance(u'abc', types.StringTypes)
True
isinstance(u'abc', str) == False
True
isinstance(u'abc', unicode) == True
True

x =set({3,4,5,6})
if type(x) is list:
    print 'a list'
elif type(x) is tuple:
    print 'a tuple'
elif type(x) is set:
    print 'a set'
else:
    print 'neither a tuple or a list'
#'a set'

type(x) in [list,tuple,set]
#True

Don't misuse types.StringType for types.StringTypes. The latter incorporates str and unicode objects.

Filter

number_list = [0.0, 0.0, 9.8, 7.6, 12.5, 0.0]
less_than_zero = filter(lambda x: x != 0, number_list)
print(less_than_zero)
#[9.8, 7.6, 12.5]

Lambda

"""
single manipulator
"""
max(map(lambda x: 3-x,[5,78,23,19]))
# -2

"""
multiple manipulators
"""
def multiply(x):
    return (x*x)
def add(x):
    return (x+x)

funcs = [multiply, add]
for i in range(5):
    value = list(map(lambda x: x(i), funcs))
value
# [0, 0]
# [1, 2]
# [4, 4]
# [9, 6]
# [16, 8]

"""
multiple argument
"""
add = lambda x, y: x + y
add(3, 5)
# 8

"""
sort list
"""
unsort = [(1, 2), (4, 1), (9, 10), (13, -3)]
unsort.sort(key=lambda x: x[1])
# [(13, -3), (4, 1), (1, 2), (9, 10)]

Reduce

"""
pass two parameters to a binary function and 
"""
from functools import reduce
product = reduce((lambda x, y: x * y), [6, 2, 3, 5])
product
#180

Map

"""
Transpose (rotate) data from rows to columns or vice versa
"""
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
map(list,zip(*matrix))
# [[1, 4, 7], [2, 5, 8], [3, 6, 9]]

Data Type Conversion

Function Description
int(x [,base]) Converts x to an integer. base specifies the base if x is a string.

chr(x)

Converts an integer to a character.

unichr(x)

Converts an integer to a Unicode character.

ord(x)

Converts a single character to its integer value.

hex(x)

Converts an integer to a hexadecimal string.

oct(x)

Converts an integer to an octal string.

None

is always returns True if it compares the same object instance, whereas == is ultimately determined by the __eq__() method.
is checks identity and cannot be overridden, while == and != are operators that can be overloaded using __eq__() and __ne__()
​None is a singleton, there is only ever one copy of the object in a Python program.
is is faster when testing against a single object, because only the pointers need to be equal. == tests for more information, the state of the objects tested needs to be the same too in that case.
x == None may evaluate to True even if x is not None but an instance of some class with its own custom equality operator object.__eq__(). it'll be called each time you use x == None, while x is None doesn't have such a hook. That means the interpreter has to execute some more Python code each time you do an equality test.

# Comparisons to singletons like None should always be done with is or is not, never the equality operators.
class Negator(object):
    def __eq__(self, other):
        return other
thing = Negator()
print thing == None    # None
print thing is None    # False

class Negator(object):
    def __eq__(self, other):
        return not other

thing = Negator()
print thing == None    # True
print thing is None    # False