Home > Back-end >  Python - What is the use of * operator on lists [duplicate]
Python - What is the use of * operator on lists [duplicate]

Time:09-21

I just came over a python program asking for output. The code was :

l=[]*100
for i in range (100):
l.append(i 1)
flag=0
k=108
for i in range(100):
if(k==l[i]):
    print("Element is present at position",i)
    flag=1
    break
if(flag==0):
print("Element is not present in the given list")

My question is what does the list l=[]*100 do?

Besides when I am executing this code the answer I get is : "Element is not present in the given list". But the answer given is 100 which I think is not possible. Someone please help

CodePudding user response:

It would have been way quicker for you to start up a Python session and try it. []*100 does nothing at all. The *100 is totally useless. If you say [0]*100, then you get a list with 100 zeros, but 100 times an empty list is still an empty list.

  • Related