Python Get Multiple Values From List

In this Python tutorial, you will learn about Python to get multiple values from a list.

In my e-commerce site, I had to implement the functionality to retrieve the product description. The description was stored in a list, and the product contains multiple details, such as size, colour, etc.

The data was in the list in a specific format, so I used slicing to get multiple descriptions of the product with a range based on the index. In this tutorial, I have explained how you can use the slicing approach to get multiple values from the list.

Also, I have explained two more approaches that do the same task but in different ways.

So let’s start,

Python Get Multiple Values From List

To get multiple values from the list in Python, there are multiple ways such as slicing, unpacking, list comprehension etc.

List Comprehension Return Two Values

List comprehension is a way to create a new list from an existing list using the for loop and expression with square brackets [ ]. Using the concept of list comprehension, you can get multiple values.

Let me show you an example; suppose you have a list of USA zipcodes as shown below.

zipcodes = ["7701", "7501", "9001", "94102"]

As you can, the list of the zip codes is in string form, so you can use the list comprehension to get a few zip codes. For example, get a zipcode that starts with the number 7.

zipcode_data = [value for value in zipcodes if value.startswith("7")]

print(zipcode_data)
Python Get Multiple Values From List

The output shows two values (zipcodes) extracted from the list ‘zipcodes’ using the list comprehension.

READ:  Python Dictionary Find Key By Value

The code part ‘zipcode_data = [value for value in zipcodes if value.startswith(“7”)]’ does three things:

  • First, it iterates over the list ‘zipcodes’ and accesses each item of the list per iteration using the ‘for value in zipcodes’.
  • Then, in each iteration, it checks if the current zipcodes start with the number ‘7’ using the condition statement ‘if value.startswith(“7”)’.
  • if the zipcode starts with the number “7”, then add that zipcode to the list by specifying the value at the beginning in the square brackets. Otherwise, skip the number if it doesn’t start with “7”.

As a result, it returns two values using the list comprehension like this [‘7701’, ‘7501’].

More than two values can be returned from the list, so this is how to use the Python list comprehension to return two values from the list.

Python Get Multiple Values From a List using Slicing

Slicing is a technique based on indexing; in slicing, you need to specify the starting and ending index of the iterable object, such as a list, to extract the range of values or items.

If you want to know more about slicing, then visit this tutorial, Slicing in Python.

Now, let’s take an example and see how slicing can help in getting multiple values from the list.

For example, you have a list of users, as shown below.

users = [
    "Lois", "Tina", "Phyllis", "Norma", "Paula", "Diana", "Annie", "Lillian", "Emily", "Robin",
    "Peggy", "Crystal", "Gladys", "Rita", "Dawn", "Connie", "Florence", "Tracy", "Edna", "Tiffany"
]

Now, you need to get the multiple users from index 5 to 15; for that, you can use the slicing shown below in the code.

print(users[5:15])
Python Get Multiple Values From a List using Slicing

The output shows that multiple users are retrieved from the list of ‘users’ using the slicing.

READ:  Python zip() Function Examples

This part of the code, users[5:15], gets all the users from index 5 to 15 in a sequence so that in the output, you can see these extracted users as a list [‘Diana’, ‘Annie’, ‘Lillian’, ‘Emily’, ‘Robin’, ‘Peggy’, ‘Crystal’, ‘Gladys’, ‘Rita’, ‘Dawn’].

This is how to get multiple values from the list using the slicing.

Python List Get Multiple Elements using Unpacking

Unpacking is a way to divide the list into more than one variable using a single assignment statement. It is useful when you already know the number of elements to extract from the list.

For example, you have a list of numbers, as shown below.

numbers=[56, 78, 98]

Suppose you need to get the three values from the list into three different variables, var1, var2, and vae2; for that, you can use the unpacking concepts as shown below.

var1, var2, var3 = numbers

print(var1, var2, var3)
Python List Get Multiple Elements using Unpacking

Look at the output. The unpacking of the list extracts all three elements from the list into three variables: var1, var2, and var3.

When you print the var1, var2 and var3, it contains three values, 56, 78 and 98, respectively.

This is how to get the multiple values from the list using the unpacking concepts. I hope from the above example you understand how to get the multiple values from the list.

Conclusion

In this Python tutorial, you learned about Python to get multiple values from the list using the list comprehension, slicing and unpacking approaches.

You may like to read: