I am new to python. i am learning some basic stuff. I was doing some operation on python list like this three_lists=[]*3 when i execute this piece of code it gives me only one empty list like this[]. Why it is not giving me 3 empty list? some what like this [],[],[]
It says right in the Python docs
s * norn * sequivalent to addingsto itselfntimes
where s is a sequence and n is an int. For example
>>> [1,2,3]*3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
This is consistent with other sequences as well, such as str
>>> 'hello'*3
'hellohellohello'
If you wanted a list of 3 empty lists you could say
>>> [[] for _ in range(3)]
[[], [], []]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With