Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How does * operator work on list in Python?
In Python, a list is an ordered sequence that can hold several object types such as integer, character, or float. The * operator has special behavior when used with lists, performing repetition instead of multiplication.
This article demonstrates how the * operator works on lists in Python through practical examples and different use cases.
Basic List Repetition
The * operator creates multiple copies of a list and joins them together. When you multiply a list by an integer n, Python repeats the entire list n times ?
# Creating a simple list
numbers = [1, 2, 3]
# Repeating the list 3 times
repeated_list = numbers * 3
print("Original list:", numbers)
print("Repeated list:", repeated_list)
Original list: [1, 2, 3] Repeated list: [1, 2, 3, 1, 2, 3, 1, 2, 3]
Reference Behavior with Mutable Objects
When lists contain mutable objects, the * operator creates references to the same objects, not copies. This can lead to unexpected behavior ?
# List containing a mutable object (another list)
original = [[1, 2]]
repeated = original * 3
print("Before modification:")
print("Original:", original)
print("Repeated:", repeated)
# Modifying the nested list
original[0].append(3)
print("\nAfter modification:")
print("Original:", original)
print("Repeated:", repeated)
Before modification: Original: [[1, 2]] Repeated: [[1, 2], [1, 2], [1, 2]] After modification: Original: [[1, 2, 3]] Repeated: [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
Using * for Unpacking
The * operator can unpack list elements, removing brackets and commas when printing ?
fruits = ['apple', 'banana', 'orange']
# Normal printing (with brackets)
print("With brackets:", fruits)
# Unpacking with * operator
print("Unpacked:", *fruits)
# Using in function calls
def display_items(item1, item2, item3):
print(f"Item 1: {item1}, Item 2: {item2}, Item 3: {item3}")
display_items(*fruits)
With brackets: ['apple', 'banana', 'orange'] Unpacked: apple banana orange Item 1: apple, Item 2: banana, Item 3: orange
Edge Cases with Different Values
The behavior of * operator changes with different numeric values ?
data = ['a', 'b']
# Multiplying by 0
print("Multiply by 0:", data * 0)
# Multiplying by negative number
print("Multiply by -2:", data * -2)
# Multiplying by 1
print("Multiply by 1:", data * 1)
# Multiplying by positive number
print("Multiply by 4:", data * 4)
Multiply by 0: [] Multiply by -2: [] Multiply by 1: ['a', 'b'] Multiply by 4: ['a', 'b', 'a', 'b', 'a', 'b', 'a', 'b']
Comparison of Different Uses
| Usage | Purpose | Example | Result Type |
|---|---|---|---|
list * n |
Repeat list | [1, 2] * 3 |
New list |
*list |
Unpack elements | print(*[1, 2]) |
Individual items |
list * 0 |
Empty list | [1, 2] * 0 |
Empty list |
list * negative |
Empty list | [1, 2] * -1 |
Empty list |
Conclusion
The * operator in Python lists serves multiple purposes: repeating lists, unpacking elements, and handling edge cases. Be careful with mutable objects as they create references, not copies, which can lead to unexpected behavior when modifying nested structures.
