How to return multiple values from a Python list

In this Python tutorial, we will see how to return multiple values from a Python list using several methods with illustrative examples.

Python list is a type of data structure that allows you to store multiple items in a single variable. Lists are created by placing a comma-separated sequence of items inside square brackets [].

For instance, we could create a list of some famous sports in the USA:

Sports_in_US = ['Soccer', 'Golf', 'Basketball', 'Baseball']

This Sports_in_US list contains four string elements, each representing a different Sport played in the US.

Returning Multiple Values from a Python list

There are several ways to return multiple values from a Python list. They are:

  • Using indexing
  • Using list slicing
  • Using list comprehension
  • Using filter() method

Method-1: Returning Multiple values from a list using Python Indexing

In Python, lists are an ordered collection of objects which are ordered and can be indexed starting from zero. That means the first element is at index 0, the second at index 1, and so on.

For instance, we could create a list of some popular neighborhoods in Chicago and we will find the two neighbors using indexing:

chicago_neighborhoods = ["Hyde Park", "Wicker Park", "Lincoln Park", "Logan Square", "West Loop"]

first_neighbor = chicago_neighborhoods[0]
second_neighbor = chicago_neighborhoods[1]
print('Two neighbors are: ', first_neighbor, second_neighbor)

The output is: As we can see we get the first two indexed elements from the list.

Two neighbors are:  Hyde Park Wicker Park
How to return multiple values in python list using indexing

Note:

  1. We can only get the element one by one because list indices only take one argument at a time.
  2. We can also, provide a negative indexed value to return values.

This way we can use the indexing method to return multiple values in a Python list.

Method-2: Returning multiple values in a List using list slicing

If we want to access more than one value at a time, we can use list slicing in Python. we specify the start and end indices, and Python will return a new list with the elements from the start-up to the end index(but not including). we can have an index jump also i.e., we will have data in the interval of the indices.

For instance, let’s create a list of the main characters in “F.R.I.E.N.D.S”, and try to get elements from it:

friends_characters = ["Rachel", "Monica", "Phoebe", "Joey", "Chandler", "Ross"]

first_three_characters = friends_characters[0:3]
print(first_three_characters)

The output is:

['Rachel', 'Monica', 'Phoebe']
How to return multiple values in python list using slicing

We can also have alternate elements from this list:

friends_characters = ["Rachel", "Monica", "Phoebe", "Joey", "Chandler", "Ross"]

first_three_characters = friends_characters[0:5:2]
print(first_three_characters)

The output is:

['Rachel', 'Phoebe', 'Chandler']
How to return multiple values in python list using slicing with index jump

Note: we can also use negative index numbers.

This way we can use list slicing to return multiple values in a Python list.

Method-3: Returning multiple values in a List using list comprehension

List comprehension is a more advanced Python feature that offers a concise way to create new lists by iterating over existing lists and applying a conditional statement.

For instance, let’s assume we have a list of episodes from “F.R.I.E.N.D.S”, and we want to return only those that start with “The One with”.

friends_episodes = ["The One with Five Steaks and an Eggplant", "The One Where Old Geller Dies",
                    "The One with the Embryos", "The One with Ross's Wedding",
                    "The One Where No One's Ready"]

episodes_starting_with_TOW = [episode for episode in friends_episodes if episode.startswith("The One with")]

print(episodes_starting_with_TOW)

The output is: In the above example, the line with the list comprehension creates a new list that includes only the episodes from friends_episodes that start with “The One with”.

['The One with Five Steaks and an Eggplant', 'The One with the Embryos', "The One with Ross's Wedding"]
How to return multiple values in python list using list comprehension

This way we can use list comprehension to make a Python list return multiple values.

Method-4: Returning multiple values using the filter() method in a Python list

The filter() function in Python creates a list of elements for which a function returns True.

Imagine we have a list of top-grossing US films, and we only want to return the films that made more than 600 million dollars. You could use filter(): Here, we will first define a function that will select the films with a gross of more than 600 million dollars and then apply this function as a parameter passed in the filter() function.

Then, we will simply create a list of all return values using the list() function.

def made_more_than_500(film):
    name, gross = film
    return gross > 600

top_grossing_films = [
    ("Titanic", 600),
    ("Star Wars: The Force Awakens", 936),
    ("Avengers: Endgame", 858),
    ("Finding Dory", 486)
]
high_grossing_films = list(filter(made_more_than_500, top_grossing_films))

print(high_grossing_films)

The output is:

[('Star Wars: The Force Awakens', 936), ('Avengers: Endgame', 858)]
How to return multiple values in python list using filter

This way we can use the filter() function to return multiple values from a Python list.

Conclusion:

Python provides us with the tools to efficiently return multiple values from a list. Through indexing, slicing, list comprehension, and filter() functions, we can extract the necessary data to fit our specific needs, making your data manipulation tasks a breeze.

You may like to read: