Learning Python — Advanced List Methods and Techniques

Learning Python — Advanced List Methods and Techniques

Advanced Python List Methods and Techniques. 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

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 in order to 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.

Memoize.py

def memoize(f):
    """ Memoization decorator for functions taking one or more arguments. """
    class memodict(dict):
        def __init__(self, f):
            self.f = f
        def __call__(self, *args):
            return self[args]
        def __missing__(self, key):
            ret = self[key] = self.f(*key)
            return ret
    return memodict(f)

# Initialize global function call variable
funcRuns = 0

# Wrap function in memoization wrapper
@memoize
def f(x):
  global funcRuns

  # Increment funcRuns every time the function is run
  funcRuns += 1
  return True

# Initialize numbers list
nums = [0,1,2,3,4,4]

# Run the list comprehension with 2 calls to f(x) per iteration
#   with 6 elements in the list and 2 calls per iteration this would 
#   normally yield 12 fuction executions. 
[f(x) for x in nums if f(x)]

# Log number of f(x) runs
print(funcRuns)

When you run the above example, you will find that the function is only run 5 times despite there being two calls to f(x) in the list comprehension and there being 6 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 underutilized 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 underutilized. 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.

CustomListSort.py

posts = [
  {
    'Post': {
      'title':'Other today post',
      'date': 43750,
      'claps': 200
    }
  }, 
  {
    'Post': {
      'title':'Python Like a Pro - Lists and Their Many Uses',
      'date': 43750,
      'claps': 525
    }
  },
  {
    'Post': {
      'title':'Yesterdays news',
      'date': 43749,
      'claps': 25
    }
  }, 
]

# Rank here returns a tuple of the days
#   since 1900 date and the number of claps
def rank(element):
  return (element['Post']['date'], 
          element['Post']['claps'])

# Sorting using our rank algorithm 
#   and reversed so the largest date
#   with the most claps is first
posts.sort(key=rank, reverse=True)

# Finally a list comprehension to tie it all together
print([post['Post']['title'] for post in posts])

The output of this will be the following list where the most recent and highest-ranking article comes first followed by other articles and yesterdays. 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']

python programming

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

Guide to Python Programming Language

Guide to Python Programming Language

Python Hello World Program - Create & Run Your First Python Program in PyCharm

Python Hello World Program - Your first step towards Python world. Learn how to create the Hello World Python program in PyCharm.

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Learn Python Programming

Learn Python Programming

Python Programming & Data Handling

Python Programming & Data Handling