Python Programming

Python lists Methods with example codes

Python Lists

You already got to know Python lists in the previous article. Have diverse examples, I am sure by now that I have made it clear to you that Python lists are one of the most important Count data structures. In this article we summarize the most important python lists Methods together again (see Table 1) and present some advanced methods for editing lists.


Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Table 1: Important functions and methods for editing lists

Function / method Importance
del l [start: end] Removes the specified list items.
n = len(l) Returns the number of elements.
l.append(x) Adds the element x to the end of the list.
l.clear() Deletes the list (corresponds to l = []).
n = l.count(x) Finds how often the element x occurs in the list.
l1.extend(l2) Adds the list l2 to the end of l1 (i.e. l1 + = l2).
iterator = filter (f, l) Returns the elements for which f (element) == true applies.
n = l.index(x) Finds the first position of x in the list.
l.insert(n, x) Inserts the element x into the list at position n.
iterator = map(f, l) Applies the function f to all elements.
x = l.pop(n) Reads the item at position n and removes it.
l.remove(x) Removes the element x from the list.
l.reverse() Reverses the list (first element last, etc.).
l.sort() Sorts the list.
iterator = zip(l1, l2) Connects the list elements in pairs to form tuples.

 

Python map function

The function map, reduce and filter presented here are not explicit ones List functions. They can be applied to any object that is iterable i.e. that contains several elements over which a loop can be formed.

In addition to Python lists, this also applies to sets, tuples, and dictionaries. For the sake of linguistic simplicity, we are only talking about Python lists here. map applies a function to all elements of a list. map delivers for reasons of efficiency not an immediate result list, but an iterator. This object can e.g. evaluated in a loop or converted into a list with list. In the following example, the function quad is first defined to get a number square. This is then applied to a list with map.

>>> def quad (x): return x * x

...

>>> l = [1, 2, 3, 4, 5]

>>> map (quad, l) # map returns an iterator

<map object at 0x1006c1250>

>>> list (map (quad, l)) # list (map (..)) returns a list

[1, 4, 9, 16, 25]





It is not necessary to first define the function to be passed to map with def. You can also specify the function as a Lambda function ad hoc. There use the lambda keyword, first a variable name and then specify the function for this variable.

>>> list (map (lambda x: x * x, l))

[1, 4, 9, 16, 25]

You can also use List Comprehension instead of map. To do this, formulate the for loop in the square brackets of a list:

>>> [x * x for x in l]

[1, 4, 9, 16, 25]

map can also process multiple lists. In this case, every list becomes that in each case passed the nth element to a function that expects as many parameters as you pass lists to map. If the lists are of different lengths, map returns so many elements like the shortest list.

#! / usr / bin / python3

def f (x, y):

return x + y

l1 = [7, 1, 4]

l2 = [10, 11, 12]

l3 = list (map (f, l1, l2))

print (l3) # output [17, 12, 16]

Python reduce function

A function is also applied to the list elements with reduce. The function but this time has to process two parameters. The function is from the left to the right applied in pairs, first to the first two list items, then on the previous result and the third element, then on the previous result and the fourth item, etc. If the list items are x1, x2, x3, etc. then equals reduce (f, l) the expression f (f (f (f (x1, x2), x3), x4), …). reduce power a singular result from a list. That also explains the name: The function reduces the list to one value. Note that in Python 3, reduce is included in the functools module that imports must become. In Python 2, however, reduce was one of the standard functions. In the following example, lambda is used to define a function that is the sum of the two Calculates parameters. reduce (…) then forms the sum of all elements.

>>> l = list (range (1, 11)); l

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> from functools import reduce

>>> reduce (lambda x, y: x + y, l)

55


Python filter

Similar to map, Python filter applies a function to each list element. The aim is this time it is not about returning the function results, but rather all of them List elements for which the filter function returns true. So it’s about all of the elements to filter from a list that meet a condition. The result is like with map an iterator, which can be evaluated with list to a list if necessary. The following example filters out all even numbers from a list. The lambda Expression returns true if there is no remainder after division by 2.

>>> l = list (range (1, 11))

>>> l

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> l = list (range (1, 11)); l

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> result = filter (lambda x: x% 2 == 0, l)

>>> list (result)

[2, 4, 6, 8, 10]

You can also achieve the same result with List Comprehension:

>>> [x for x in l if x% 2 == 0]

[2, 4, 6, 8, 10]


Python zip

zip connects related elements of several lists to form tuples. If two lists are passed to zip, the result consists of tuples, each of which is the first, contain second, third elements of the lists. Like map and filter also delivers zip an iterator that you evaluate with list for a list or with set for a set can. zip can also process more than two lists if necessary. If the lists have different numbers of elements, determines the list with the smallest Number of elements, the result.

>>> l1 = list (range (1, 11)); l1

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> l2 = list ('abcdefghij'); l2

['a', 'b', 'c', 'd', 'e', ​​'f', 'g', 'h', 'i', 'j']

>>> list (zip (l1, l2))

[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'),

(6, 'f'), (7, 'g'), (8, 'h'), (9, 'i'), (10, 'j')]

With dict (zip (keylist, valuelist)) you can choose from two lists with keys and Create a dictionary:

>>> dict (zip (l1, l2))

{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e',

6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j'}

 

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

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

Back to top button