![]() |
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 ListBelow are some of the ways by which we can use starred expression to unpack a list in Python:
Using Starred Expression in AssignmentIn this method, the starred expression is used in an assignment to unpack the first element ( Python3
Output
First element: 1 Rest of the elements: [2, 3, 4, 5] Unpack a List Inside a Function CallIn this method we use the starred expression when calling a function. The Python3
Output
Average Grade: 90.0 Unpack a List Multiple Starred ExpressionsIn this method, multiple starred expressions are used to unpack elements from two lists ( Python3
Output
Combined Team: [['Alice', 'Bob'], 'Charlie'] Captain of Team B: David Unpacking in Nested ListIn this method, unpacking is in the context of nested lists ( Python3
Output
Row 1: [1, 2, 3] Row 2: [4, 5, 6] Row 3: [7, 8, 9] |
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |