×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python Unpack List

Last Updated : May 03, 2025

Unpacking Python list means extracting elements from a list into variables. In this chapter, we will learn how to unpack a list using different ways with the help of examples.

Unpacking List into Variables

You can directly unpack the elements of a list into individual variables using multiple assignment.

Example

In this example, we are unpacking a list of student names into separate variables.

# Creating a list of names
names = ["Amit", "Bhavna", "Chirag"]

# Unpacking the list into variables
name1, name2, name3 = names

# Displaying the results
print(name1, name2, name3)

The output of the above code would be:

Amit Bhavna Chirag

Unpacking List with the Star Operator (*)

The star operator (*) can be used to unpack the remaining elements of a list into a variable. This is useful when you don't know the exact number of elements in a list.

Example

In this example, we unpack the first two elements of the list into separate variables, while the rest of the elements are stored in a third variable using the star operator.

# Creating a list of names
names = ["Amit", "Bhavna", "Chirag", "Deepak", "Esha"]

# Unpacking with the star operator
first_name, second_name, *other_names = names

# Displaying the results
print(first_name, second_name)
print("Other names:", other_names)

The output of the above code would be:

Amit Bhavna
Other names: ['Chirag', 'Deepak', 'Esha']

Unpacking Nested Lists

If you have a nested lists, you can unpack the inner elements as well.

Example

In this example, we unpack a list of lists where each sublist contains a student's name and their score.

# Creating a list of lists
students_scores = [["Amit", 85], ["Bhavna", 78], ["Chirag", 92]]

# Unpacking nested lists
for student, score in students_scores:
    print(f"{student} scored {score} marks")

The output of the above code would be:

Amit scored 85 marks
Bhavna scored 78 marks
Chirag scored 92 marks

Unpacking with the enumerate() Function

Unpacking can also be done using the enumerate() to access both the index and the elements in a list.

Example

In this example, we are using enumerate() to get both the index and the value while unpacking a list of cities.

# Creating a list of cities
cities = ["Mumbai", "Delhi", "Bengaluru"]

# Using enumerate to unpack list and get index
for i, city in enumerate(cities):
    print(f"Index {i}: {city}")

The output of the above code would be:

Index 0: Mumbai
Index 1: Delhi
Index 2: Bengaluru

Unpacking List in Function Arguments

You can also unpack lists when passing them as arguments to functions.

Example

In this example, we are passing a list to a function and unpacking the elements inside the function.

# Function to display student information
def display_student_info(name, age, grade):
    print(f"Name: {name}, Age: {age}, Grade: {grade}")

# List of student data
student_data = ["Ananya", 20, "A+"]

# Unpacking the list as function arguments
display_student_info(*student_data)

The output of the above code would be:

Name: Ananya, Age: 20, Grade: A+

Unpacking Lists with Multiple Variables

You can unpack a list into multiple variables, even when some elements are not needed. You need to use the underscore (_) to discard unwanted values.

Example

In this example, we are unpacking a list of city names and only keeping the first two elements, discarding the rest using an underscore.

# Creating a list of cities
cities = ["Mumbai", "Delhi", "Bengaluru", "Kolkata"]

# Unpacking the first two elements and ignoring the rest
city1, city2, _ , _ = cities

# Displaying the results
print(city1, city2)

The output of the above code would be:

Mumbai Delhi

Exercise

Select the correct option to complete each statement about unpacking lists in Python.

  1. In Python, to unpack a list into individual variables, you use the ___ syntax.
  2. If a list contains more elements than the variables you are unpacking, you can use ___ to capture extra elements.
  3. Given lst = [1, 2, 3, 4], using the syntax a, *b = lst, ___ will hold the value of b.

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.