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.
1. Retrieving a certain portion of a list
Let’s say 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.
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
elements = slice(4 ,10 ,1)
list2 = list1[elements]
print(list2)
Output:
[5, 6, 7, 8, 9, 10]
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
mylist = [1,2,3,4,5]
def multi(x):
return 10*x
list(map(multi, mylist))
In this case, the map() function applies the user define function multi() on all the elements of mylist[]
and returns the values.
Output
[10, 20, 30, 40, 50]
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
str="strings are cool"
print(str[::-1])
Output
looc era sgnirts
Want to stay tuned with more similar exciting articles on Python & Data Science – do consider becoming a medium member using my referral link: https://pranjalai.medium.com/membership.
4. Iterating over multiple lists simultaneously
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
colour = ["red", "yellow", "green"]
fruits = ['apple', 'banana', 'mango']
price = [60,20,80]
for colour, fruits, price in zip(colour, fruits, price):
print(colour, fruits, price)
Here we have iterated through three lists to print the values inside them.
Output
red apple 60
yellow banana 20
green mango 80
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
dictionary = {"a": [1,2,3], "b":[4,5,6]}
numbers = sum(dictionary.values(), [])
print(numbers)
Output
[1, 2, 3, 4, 5, 6]
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
items = ["footballs", "bats", "gloves"]
price = [100, 40, 80]
dictionary = dict(zip(items, price))
print(dictionary)
Output
{'footballs': 100, 'bats': 40, 'gloves': 80}
Conclusion
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.
Before you go…
If you liked this article and want to stay tuned with more exciting articles on Python & Data Science – do consider becoming a medium member using my referral link: https://pranjalai.medium.com/membership.
Also, feel free to subscribe to my free newsletter: Pranjal’s Newsletter.