Advanced Python List Methods and Techniques
One of the most powerful data structures in Python is the list; here are some more advanced list uses
One of the most powerful data structures in Python is the list. I didn’t really believe that until I started working through the documentation and realized just how much work had been put into building the list data structure.
Python lists natively support being utilized as queues, stacks, and arrays. This is why, to use Python like a pro, it is important to have a good understanding of lists.
In this article, we will cover list comprehensions, the zip
method, and the sort
method.
List Comprehensions
Comprehensions are an advanced feature of Python lists that can help make code cleaner and easier to read.
A composition is simply a way of performing a series of operations on a list using a single line. Comprehensions are denoted typically by the use of a for
statement inside brackets.
Here is a template for list comprehensions:newList = [returned_value for item in list condition_logic ]
Pulling out specific elements
List comprehensions can be used to pull out certain elements that meet specific criteria. In the following example, we use a comprehension to pull out all the even numbers from a list.# Create a list of numbers from 0 - 49
numRange = range(0,50)# Pull out all the numbers that are even
evenNums = [num for num in numRange if num % 2 == 0 ]
In the above example, reading from left to right we are creating a new list with the num
that is returned from the for loop where the remainder (%
modulo) of the num
divided by two is equal to zero.
This is a common use case where all the even numbers need to be pulled from a list.
Perform an operation on elements
List comprehensions can be used to perform operations on elements in a list. The following example shows how all the elements of a list could be squared.# Create a list of numbers from 0 - 49
numRange = range(0,50)# Pull out all the numbers that are even
evenNums = [num * num for num in numRange]
Limiting function calls using memoization
This is one is a particularly useful bit of code that can save you from making expensive function calls more than necessary. The source was this post on Stack Overflow.
Memoization is the process of storing values in memory so that we don’t need to recompute the results later.
The case goes like this: you have a list that may potentially contain duplicate data or the function needs to be run to both check the output and to return the value.
Here, memoization can help by using a dictionary to keep track of the results of function calls with the same input parameters.
When you run the above example, you will find that the function is only run five times despite there being two calls to f(x)
in the list comprehension and there being six elements in the list.
It is only getting called once per unique number. Otherwise, the cached value is served up. If the function call is expensive, you can greatly speed up your code by memoizing the results.
This works well for reasonably sized lists to provide a speed improvement, but it could cause issues for very large lists as all the inputs/outputs are cached while the function is in scope, requiring the extensive use of memory to store values.
List Advanced Methods
Along with comprehensions, there are several other helpful methods available for lists. Here are a few that may be under-utilized or otherwise unknown.
Zip(list, list2, …)
The zip method is used to combine multiple lists in Python into tuples. If the two lists are not the same length, then the longer of the two lists would be truncated to the length of the shorter.first_names = ['John', 'Jeff', 'Chris']
last_names = ['Wick', 'Chen', 'Test', 'Truncated']names = zip(first_names, last_names)for name in names:
print(name)# Outputs:
('John', 'Wick')
('Jeff', 'Chen')
('Chris', 'Test')
List.Sort(key=func, reversed=T/F)
I know. It seems strange putting the sort method into an article like this. I put it here because I feel that the use of sort with custom ranking functions is super under-utilized.
Here is my attempt at making a small sorting function that will return the top posts for a given day. I made sure to use a list comprehension at the end as well.
The output of this will be the following list where the most recent and highest-ranking article comes first, followed by other articles and yesterday’s.
This does not account for when yesterday's articles were performing well enough to necessitate a top spot still, but I think the point is made.['Python Like a Pro - Lists and Their Many Uses',
'Other today post',
'Yesterdays news']