# 6 Easy Ways to Extract Elements From Python Lists

## Problem Formulation and Solution Overview

In this article, you’ll learn how to extract data from List elements in Python.

To make it more fun, we have the following running scenario:

The Finxter Academy has been keeping tabs on an up-and-coming stock, MediTech. Each Sunday, the prices for the previous week are updated and saved to a List. You have been asked to extract this data.

π¬ Question: How would we write code to extract this data?

We can accomplish this task by one of the following options:

## Method 1: Use Slicing

This example uses Python’s infamous slicing method to carve out (extract) stock prices from Monday (19.71) to Friday (20.12).

prices  = [17.91, 19.71, 18.55, 18.39, 19.01, 20.12, 19.87]
mon_fri = prices[1:6]
print (mon_fri)

Above declares a List containing the previous week’s stock prices (Sunday-Saturday) and saves to prices.

To extract this data, slicing is applied. First, we set the start position [1:], (the 2nd element). Then, we enter a colon [:] and a stop position ([:6]). The stop position is always (position-1). The results save to mon_fri and are output to the terminal.

## Method 2: Use List Index

Another option is to use the List Index to extract Wednesday’s stock price (18.39).

prices    = [17.91, 19.71, 18.55, 18.39, 19.01, 20.12, 19.87]
wed_price = prices[3]
print(wed_price)

Above declares a List containing the previous week’s stock prices (Sunday-Saturday) and saves to prices.

Next, the element containing Wednesday’s stock price is extracted by entering the appropriate location (prices[3]). The result saves to wed_price and is output to the terminal.

## Method 3: Use Simple List Comprehension

This option uses List Comprehension to loop through and extract each List element (stock price).

prices     = [17.91, 19.71, 18.55, 18.39, 19.01, 20.12, 19.87]
all_prices = [x for x in prices]
print(all_prices)

Above declares a List containing the previous week’s stock prices (Sunday-Saturday) and saves to prices.

Next, List Comprehension is used to loop and extract all price values. The result saves to all_prices and is output to the terminal.

## Method 4: Use List Comprehension with Condition

You can also use a list comprehension with condition to filter out a number of list elements that meet the condition. For example, the expression [x for x in my_list if x>z] filters out all values from my_list that are larger than a given value z.

Here’s another example:

prices = [17.91, 19.71, 18.55, 18.39, 19.01, 20.12, 19.87]
high_prices = [x for x in prices if x>18]
print(high_prices)

Output:

[19.71, 18.55, 18.39, 19.01, 20.12, 19.87]

## Method 5: Use Enumerate

This option uses enumerate() to convert an object (List, Tuple, etc.) into an enumerate object for easy access to List values. For this example, stock prices for Monday, Wednesday and Friday are retrieved.

prices     = [17.91, 19.71, 18.55, 18.39, 19.01, 20.12, 19.87]
three_days = [wday[1] for wday in enumerate(prices) if wday[0] in [1, 3, 5]]
print(three_days)

Above declares a List containing the previous week’s stock prices (Sunday-Saturday) and saves to prices.

Next, List Comprehension is used in conjunction with enumerate() to extract the appropriate values based on the indices in the sub-list ([1, 3, 5]). The result saves to three_days and is output to the terminal.

## Method 6: Use NumPy Array()

This option calls in the NumPy library to exact List elements using array(). For this example, the stock prices for Sunday and Saturday are retrieved.

import numpy as np
prices  = [17.91, 19.71, 18.55, 18.39, 19.01, 20.12, 19.87]
sat_sun = [0, 6]
print(list(np.array(prices)[sat_sun]))

Above, the NumPy library is called in. If this is not installed, click here for installation instructions.

Next, a List containing the previous week’s stock prices (Sunday-Saturday) and saves to prices is declared.

Then, a sub-list is created containing the data indices to extract ([0, 6]). In this case, the stock prices for Sunday and Saturday and passed as an argument to np.array(). The results are output to the terminal.

## Summary

These methods of extracting data from Lists should give you enough information to select the best one for your coding requirements.

Good Luck & Happy Coding!

## Programmer Humor

There are only 10 kinds of people in this world: those who know binary and those who donβt.