10 Awesome Python List Tricks
In Python, an array is a collection of values that belong to the same data type. Arrays are useful for storing and manipulating large amounts of data efficiently.
1. Reverse a List
To reverse a list, you can use the reverse()
method. This method reverses the order of the elements in the list.
a = [1, 2, 3, 4, 5]
a.reverse()
print(a)
# [5, 4, 3, 2, 1]
2. Sort a List by a key
To sort a list by a key, you can use the sorted()
function. This function takes a list and a key as arguments and returns a new list sorted by the key.
a = [1, 2, 3, 4, 5]
b = sorted(a, key=lambda x: x % 2)
print(b)
# [2, 4, 1, 3, 5]
3. List comprehension to create a list
To create a list, you can use list comprehension. This is a concise way to create a list by iterating over an iterable object.
numbers = [i for i in range(5)]
squares = [n ** 2 for n in numbers]
print(squares)
# [0, 1, 4, 9, 16]
# List comprehension with if condition
squares = [n ** 2 for n in numbers if n % 2 == 0]
print(squares)
# [0, 4, 16]
4. Use enumerate()
to get the index of an element
To get the index of an element in a list, you can use the enumerate()
function. This function takes a list as an argument and returns an enumerate object. You can convert this object into a list of tuples using the list()
function.
a = [1, 2, 3, 4, 5]
b = list(enumerate(a))
print(b)
# [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]
5. Use zip()
to iterate over two lists simultaneously
To iterate over two lists simultaneously, you can use the zip()
function. This function takes two lists as arguments and returns an iterator of tuples.
a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]
for i, j in zip(a, b):
print(i, j)
# 1 6
# 2 7
# 3 8
# 4 9
# 5 10
6. Use map()
to apply a function to all elements of a list
To apply a function to all elements of a list, you can use the map()
function. This function takes a function and an iterable object as arguments and returns a map object. You can convert this object into a list using the list()
function.
a = [1, 2, 3, 4, 5]
b = list(map(lambda x: x ** 2, a))
print(b)
# [1, 4, 9, 16, 25]
7. Use filter()
to filter elements of a list
To filter elements of a list, you can use the filter()
function. This function takes a function and an iterable object as arguments and returns a filter object. You can convert this object into a list using the list()
function.
a = [1, 2, 3, 4, 5]
b = list(filter(lambda x: x % 2 == 0, a))
print(b)
# [2, 4]
8. Use reduce()
to reduce a list to a single value
To reduce a list to a single value, you can use the reduce()
function. This function takes a function and an iterable object as arguments and returns a single value.
from functools import reduce
a = [1, 2, 3, 4, 5]
b = reduce(lambda x, y: x + y, a)
print(b)
# 15
9. Use any()
to check if any element of a list is true
To check if any element of a list is true, you can use the any()
function. This function takes a list as an argument and returns True
if any element of the list is true.
a = [True, False, False]
b = any(a)
print(b)
# True
10. Use all()
to check if all elements of a list are true
To check if all elements of a list are true, you can use the all()
function. This function takes a list as an argument and returns True
if all elements of the list are true.
a = [True, False, False]
b = all(a)
print(b)
# False
Bonus: Use collections.Counter()
to count the frequency of elements in a list
To count the frequency of elements in a list, you can use the collections.Counter()
function. This function takes a list as an argument and returns a dictionary with the elements of the list as keys and their frequencies as values.
from collections import Counter
a = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
b = Counter(a)
print(b)
# Counter({1: 2, 2: 2, 3: 2, 4: 2, 5: 2})
Conclusion
List is a very useful data structure in Python. In this article, we have seen some useful tricks to work with lists in Python. You might also want to check about 10 awesome string tricks and 10 awesome dictionaries tricks in Python.