
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How does 'in' 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.
In this article, we will show you how the * operator works on a list in python. So we will show you different examples to understand how * works on a python list in the below section ?
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
Below are the different examples to understand how * works on a python list:
Repetition Operator (*) on List
Python List also includes the * operator, which allows you to create a new list with the elements repeated the specified number of times. 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.
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
When you run the program, it will show this output-
[5, 6, 7, 5, 6, 7]
Repetition Operator (*) as Reference
Items in the sequences/list are not copied instead, they are referred to several/multiple times.
When you use *, Python does not always make a new copy of the list elements. Instead, it keep pointing to the same objects. This can cause problems if you only edit one item.
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 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 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
After running the program, you will get this result:
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 * to Unpack in 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.
Example
The following Python program shows how to use the * operator to unpack-
# input list inputList = ['TutorialsPoint', 'Python', 'Codes', 'hello', 5, 'everyone', 10, 5.3] # Converting list elements to string using map() print('Without Using * Operator :') print(' '.join(map(str,inputList))) # Using the asterisk (*) operator print('Using * operator : ') print (*inputList)
Output
This output will be displayed when the program runs:
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
When a value less than or equal to 0 is provided, an empty sequence of the same type is returned. When you provide a number after *, Python knows how many times to repeat the list. For example, ['a'] * 5 repeats 'a' five times.
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
You will see this result after executing the program:
[]
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
The program gives this output when it is run-
[]
As 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.