There are many ways to use Python’s List and Lambda. Here I am going to show some of useful tips and tricks. So lets first start with Lists>

Below we use most of the time with List.

>>> a = [66.6, 333, 333, 1, 1234.5]
>>> print a.count (333), a.count (66.6), a.count ('x')
2 1 0
>>> a.insert (2, -1)
>>> a.append (333)
>>> a
[66.6, 333, -1, 333, 1, 1234.5, 333]
>>> a.index (333)
1
>>> a.index (333,2)
3
>>> a.remove (333)
>>> a
[66.6, -1, 333, 1, 1234.5, 333]
>>> a.reverse ()
>>> a
[333, 1234.5, 1, 333, -1, 66.6]
>>> a.sort ()
>>> a
[-1, 1, 66.6, 333, 333, 1234.5]

Use List as Stack

With these easy-to-use methods, we can utilize lists as stacks. The last member of Stacks to join is the first member to be fetched (“last-in, first-out” rule). To append a member to the top of the stack you can use append(). To remove a member from the top of the stack, you can use pop() (Note: you do not need to include parameters). Examples are as follows:

>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
>>> stack.pop()
6
>>> stack
[3, 4, 5]

Use Lists as Queues

You can also use List as queues. The characteristic of Queues is that the first member to join will be the first one to be removed (FIFO) “first-in, first-out” rule. To add a member to the back end of the queue can use append (), to remove a member from the front of the queue can use pop(0), remember that the parameter is 0. Examples are as follows:

>>> queue = ["Eric", "John", "Michael"]
>>> queue.append ("Terry") # Terry arrives
>>> queue.append ("Graham") # Graham arrives
>>> queue
['Eric', 'John', 'Michael', 'Terry', 'Graham']
>>> queue.pop (0)
'Eric'
>>> queue.pop (0)
'John'
>>> queue
['Michael', 'Terry', 'Graham']

Functional programming tools for Lists

There are three built-in utility functions that are useful for lists: filter (), map (), and reduce ().

“filter (function, sequence)”: This function will return a sequence (members of the same data type), the members of this sequence are the sequence inside the members, one by one passed to the my_function (item) represents the function returned by value. This function has its code of filtering incoming sequences, as the following example shows:

>>> def f(x): return x%2 != 0 and x%3 != 0
...

>>> filter(f, range(2,25))
[5, 7, 11, 13, 17, 19, 23]

“map (function, sequence)” calls function (item) for each member in the sequence and returns the result returned by the individual member after the call. For example, to calculate a series of cube values, we can do so:

>>> def cube(x): return x*x*x
...
>>> map(cube,range(1,11))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

We can also pass more than one sequence. If you pass in more than one sequence, the first argument of the function name should be a function that can handle multiple arguments, and the system will put the members of each sequence into the function if the two sequence length is not equal, the deficiencies will use “None” to make up (If the first argument to a function name is “None”). So we can use “map (None, list1, list2)” a tool function to easily convert the two sequences into a sequence of a pair of member combinations. Look at the example:

>>> seq = range(8)
>>> def square(x): return x*x
...
>>> map(square,seq)
[0, 1, 4, 9, 16, 25, 36, 49]
>>> map(None,seq,map(square,seq))
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49)]

“reduce (func, sequence)” takes advantage of the first two members of the sequence when the parameter calls func, then the resulting return value is then passed along with the next member to func until the end of the entire sequence. The following example calculates the sum of 1 to 10:

>>> def add (x, y): return x + y
...
>>> reduce(add, range(1,11))
55

List Comprehensions(Return the entire list): List comprehensions provides a concise way of making a list without using map(), filter() or lambda. A list comprehension is usually an expression followed by a for statement(zero or more for or if statements). The returned list is a result of executing expression under the for and if statements. If the result of expression is a tuple, it must be enclosed in parenthesis “()”.

>>> vec = [2,4,6]
>>> [4*x for x in vec]
[8, 16, 24]

>>> [4*x for x in vec if x > 3]
[16, 24]

>>> [4*x for x in vec if x < 3]
[8]

>>> [{x: x ** 2} for x in vec]
[{2: 4}, {4: 16}, {6: 36}]

>>> vec1 = [2, 4, 6]
>>> vec2 = [4, 3, -9]
>>> [x * y for x in vec1 for y in vec2]
[8, 6, -18, 16, 12, -36, 24, 18, -54]

>>> [x + y for x in vec1 for y in vec2]
[6, 5, -7, 8, 7, -5, 10, 9, -3]

Uses of lambda

Lambda helps for eliminating the need to define the function of the process to make the code more streamlined. Using lambda does not need to be named. Using lambda at some point makes the code easier to understand.The lambda statement, before the colon is a parameter, you can have more than one, separated by commas. A lambda statement is actually a function object:

>>> g = lambda x: x ** 2
>>> print g
<function <lambda> at 0x7f419c2d22a8>

There are several well-defined global functions(such as filter, map, and reduce etc.) in Python that are easy to use.

>>> foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]

>>> print filter (lambda x: x% 3 == 0, foo)
[18, 9, 24, 12, 27]

>>> print map (lambda x: x * 2 + 10, foo)
[14, 46, 28, 54, 44, 58, 26, 34, 64]

>>> print reduce (lambda x, y: x + y, foo)
139

A map can be written as:

>>> print [x * 2 + 10 for x in foo]
[14, 46, 28, 54, 44, 58, 26, 34, 64]

>>> print [x for x in foo if x% 3 == 0]
[18, 9, 24, 12, 27]

When using lambda, you need to be aware of the scope of the problem. See below one issue with using Lambda which is not giving expected result

>>> fs = [(lambda n: i + n) for i in range (10)]
>>> fs [3] (4)
13
>>> fs [4](4)
13
>>> fs [5](4)
13

The reason for not achieving the expected result is that i in the lambda uses a global variable outside the anonymous function. Lets edit a bit:

>>> fs = [(lambda n, i = i: i + n) for i in range (10)]
>>> fs [3] (4)
7
>>> fs [4](4)
8
>>> fs [5](4)
9

Leave a Reply

Your email address will not be published. Required fields are marked *