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.

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.

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

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.

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.

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.

You can also achieve the same result with List Comprehension:

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.

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


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...

Related Articles

Leave a Reply

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

Back to top button