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. In other programming languages, a list is equivalent to an array

Repetition Operator(*)

A repetition operator is supported by sequence datatypes (both mutable and immutable). * The repetition operator * creates several copies of that object and joins them together. When used with an integer, * performs multiplication, but when used with a list, tuple, or strings, it performs repetition

In this article, we will show you how the * operator works on a list in python. Below are the different examples to understand how * works on a python list −

  • Repetition Operator(*) on List Items

  • How Repetition Operator(*) works as a reference

  • Using the * operator to unpack a function.

  • When the Repetition Value is given 0

Repetition Operator(*) on List Items

Python List also includes the * operator, which allows you to create a new list with the elements repeated the specified number of times.

The following program repeats the list the given number of times using the * operator −

# input list inputList = [5, 6, 7] # Repeating the input list 2 times using the * operator print(inputList * 2)

Output

On executing, the above program will generate the following output −

[5, 6, 7, 5, 6, 7]

Here, we took a list of random values and multiplied it twice with the * operator, so that the output consists of the given list repeated twice.

How Repetition Operator (*) works as a reference

Items in the sequences/list are not copied instead, they are referred to several/multiple times.

inputList_1=[[3]] inputList_2= inputList_1*3

Example

List inputList_2 elements correspond to the same element in list inputList_1. As a result, changing any of the elements in list inputList_1 will change the elements in list inputList_2.

# Input list inputList_1=[4] # Multiplying the whole input list_1 three times using the Repetition Operator inputList_2= inputList_1*3 print('The Input list 1 without modification : ',inputList_1) print('The Input list 2 without modification : ',inputList_2) # Modifying the first element value of the input list with 20 inputList_1[0]= 20 #printing the output print('The Input list 1 after modification : ',inputList_1) print('The Input list 2 after modification : ',inputList_2)

Output

The Input list 1 without modification : [4]
The Input list 2 without modification : [4, 4, 4]
The Input list 1 after modification : [20]
The Input list 2 after modification : [4, 4, 4]

The first list has only one element, which is 4, then we multiplied it three times with the repetition operator (*) and saved it in another list (input list 2). When we change the first list's value, we can see that the second list elements change without changing the second list (input list 2). This means that items/elements in the sequences/list are referred to several/multiple times rather than copied.

Using * operator to unpack a function

This method is quite handy when printing data in a raw format (without any commas and brackets ). Many programmers attempt to remove commas and brackets by converging functions, thus this simple prefix asterisk can fix your problem in unpacking them.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input list and give it some random values.

  • To print list elements separated by spaces without brackets [] first we convert the list to a string by passing the str and list as arguments to the map () function. It converts each element of the list to the string type and returns the resultant list of items. The join() function (join() is used to join elements of a sequence that are separated by a string separator) is used to convert the result list to a string.

  • Instead of the previous method, we can use the asterisk operator (*) to print the list separated by spaces

Example

# input list inputList = ['TutorialsPoint', 'Python', 'Codes', 'hello', 5, 'everyone', 10, 5.3] # Converting list elements to string using map() # Applying join() function to convert list to string # Without using the asterisk (*) operator print('Without Using * Operator :') print(' '.join(map(str,inputList))) # Using the asterisk (*) operator print('Using * operator : ') print (*inputList)

Output

Without Using * Operator :
TutorialsPoint Python Codes hello 5 everyone 10 5.3
Using * operator :
TutorialsPoint Python Codes hello 5 everyone 10 5.3

Output remains the same using both ways.

When Repetition Value is given 0

When a value less than or equal to 0 is provided, an empty sequence of the same type is returned.

Example 1

The following program returns an empty list when input is multiplied with 0 −

# input list inputList = [5, 6, 7] # returning an empty list when repetition Value is given 0 print(inputList * 0)

Output

On executing, the above program will generate the following output

[]

We used 0 as the repetition value here, so we get an empty list because something multiplied by 0 equals 0 (empty)

Example 2

The following program returns an empty list when input is multiplied with any number less than 0

# input list inputList = [5, 6, 7] # returning an empty list when repetition Value is given -4 print(inputList * -4)

Output

On executing, the above program will generate the following output

[]

Because the * operator only accepts positive values, we get an empty list when we pass -4 as the repetition value. If there are any negative values, it cannot multiply them and thus returns an empty list.

Conclusion

This article covered every case involving the * repetition operator on the list. We also talked about how it will behave in various scenarios. We learned how to use the * operator to print list elements that are separated by spaces. This can be useful in a variety of situations, such as programming contests, to save time instead of writing numerous functions.

Updated on: 03-Nov-2023

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements