CAN YOU DO list list in Python?

Photo by Zami from Pexels

One of the most common data structures that we use in Python is lists and we use this data structure in almost every project that we make in Python. Therefore, having knowledge of some tricks related to lists will not only make your job easier but sometimes they are more efficient than the straightforward code that we frequently write.

In this article, I will talk about 6 such tips and tricks using python lists that will definitely make your life easier. These tricks include some inbuilt functions in Python and some list comprehensions.

Let’s sy we want to retrieve a certain portion of a list and make a new list out of it. We can achieve that using the slice[] method in Python. This method takes the starting index value, ending index value and the increment order as the arguments and retrieves the elements in that order.

Output:

This method can be used to reverse the list also but for that, we have another cool method that we will see in the 3rd point of this article.

2. Perform similar operation on every element of the list

If you want to perform a set of operations on every element of a list you can do that using the map[] function in Python.

You only need to write your function including those operations and then use the map[] function which takes a function and an iterable[list in this case] as arguments and performs the operation on all the elements of the list.

Example

In this case, the map[] function applies the user define function multi[] on all the elements of mylist[] and returns the values.

Output

3. Reversing a list

It is a fairly common trick that is known to almost all python programmers but it is worth starting with. In this case, we use the [] operator to traverse through a list in the reverse direction by setting the third parameter to -1.

Example

Output

Let’s say you have two lists whose elements you want to access simultaneously. The zip[] function in Python allows you to do that using one line of code. This function takes multiple lists as the arguments and iterates through them at the same time. The iteration stops when the smaller list is exhausted.

The best part about the zip function is that it can be used to iterate through multiple lists at the same time.

Example

Here we have iterated through three lists to print the values inside them.

Output

5. Joining lists inside a dictionary

This trick will be helpful when the value of the key-value pair in a dictionary is a list. If we want to combine all the lists that are present as values in the dictionary we can take the help of the built-in sum[] function to do that.

Example

Output

6. Merging two lists to form a list

Suppose you have two lists and you want to merge the two lists to form a dictionary i.e. the elements from one list will be the keys and the elements from the other lists will be the values. Using the zip[] function in python we can do that task with one line of code.

Example

Output

These were a few tips and tricks that you can apply while working with lists to make your job easier. These tricks not only require lesser lines of code, some of them are helpful for improving performance also.

Along with these tricks, you can use list comprehensions to make your code more compact and more efficient. I will be discussing more tips and tricks related to python in our upcoming articles.

Stay tuned!! for more upcoming tips related to Python and Data Science.

Page 2

Photo by Zami from Pexels

One of the most common data structures that we use in Python is lists and we use this data structure in almost every project that we make in Python. Therefore, having knowledge of some tricks related to lists will not only make your job easier but sometimes they are more efficient than the straightforward code that we…

A list is an ordered and mutable Python container, being one of the most common data structures in Python. To create a list, the elements are placed inside square brackets [[]], separated by commas.

Video liên quan

Chủ Đề