Open In App

Starred Expression in Python

Last Updated : 20 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Python, the starred expression is a feature that allows for the unpacking of elements from iterable like lists. This feature is particularly useful when the number of elements is variable or when working with function arguments. One common use case of the starred expression is to unpack elements from a list. In this article, we will study to use of Starred Expression To Unpack a List in Python.

Starred Expression to Unpack a List

Below are some of the ways by which we can use starred expression to unpack a list in Python:

  • Using Starred Expression in Assignment
  • Unpacking Inside a Function Call
  • Unpacking Multiple Starred Expressions
  • Unpacking in Nested Lists

Using Starred Expression in Assignment

In this method, the starred expression is used in an assignment to unpack the first element (first) and the rest of the elements (rest) from a list (numbers). The first variable captures the first element, and the rest variable is assigned a list containing the remaining elements.

Python3




# Example list
numbers = [1, 2, 3, 4, 5]
 
# Unpack the list using starred expression
first, *rest = numbers
 
# Print the results
print("First element:", first)
print("Rest of the elements:", rest)


Output

First element: 1
Rest of the elements: [2, 3, 4, 5]


Unpack a List Inside a Function Call

In this method we use the starred expression when calling a function. The calculate_average function takes individual arguments, and the grades are unpacked using the starred expression within the function call. This enables passing a variable number of arguments to the function.

Python3




# Example list
grades = [90, 85, 88, 92, 95]
 
# Define a function that takes individual arguments
 
def calculate_average(first, *others):
    total = first + sum(others)
    average = total / (1 + len(others))
    return average
 
# Unpack the list inside the function call
average_grade = calculate_average(*grades)
 
# Print the result
print("Average Grade:", average_grade)


Output

Average Grade: 90.0


Unpack a List Multiple Starred Expressions

In this method, multiple starred expressions are used to unpack elements from two lists (team_a and team_b). The combined_team variable captures all elements except the last one from team_a, and the captain_b variable captures the first element from team_b.

Python3




# Example lists
team_a = ["Alice", "Bob"]
team_b = ["Charlie", "David"]
 
# Unpack both lists using starred expressions
*combined_team, captain_b = team_a, *team_b
 
# Print the results
print("Combined Team:", combined_team)
print("Captain of Team B:", captain_b)


Output

Combined Team: [['Alice', 'Bob'], 'Charlie']
Captain of Team B: David


Unpacking in Nested List

In this method, unpacking is in the context of nested lists (matrix). Each row of the matrix is unpacked using a starred expression, resulting in individual lists (row1, row2, and row3) that represent the rows of the matrix.

Python3




# Example list with nested lists
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
 
# Unpack the nested lists using starred expressions
*row1, = matrix[0]
*row2, = matrix[1]
*row3, = matrix[2]
 
# Print the results
print("Row 1:", row1)
print("Row 2:", row2)
print("Row 3:", row3)


Output

Row 1: [1, 2, 3]
Row 2: [4, 5, 6]
Row 3: [7, 8, 9]




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads