Starred Expression in Python
Last Updated :
20 Feb, 2024
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
numbers = [ 1 , 2 , 3 , 4 , 5 ]
first, * rest = numbers
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
grades = [ 90 , 85 , 88 , 92 , 95 ]
def calculate_average(first, * others):
total = first + sum (others)
average = total / ( 1 + len (others))
return average
average_grade = calculate_average( * grades)
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
team_a = [ "Alice" , "Bob" ]
team_b = [ "Charlie" , "David" ]
* combined_team, captain_b = team_a, * team_b
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
matrix = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 ]]
* row1, = matrix[ 0 ]
* row2, = matrix[ 1 ]
* row3, = matrix[ 2 ]
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]
Share your thoughts in the comments
Please Login to comment...