Answers for "python [a]*b means [a,a,...b times]"

0

python [a]*b means [a,a,...b times]

The [0] * x creates a list with x elements. So,
>>> [ 0 ] * 5
   gives [0,0,0,0,0]

******** warn:they all point to the same object.
This is cool for immutables like integers but a pain for things like lists.
>>> t = [[]] * 5
>>> t
[[], [], [], [], []]
>>> t[0].append(5)
>>> t
[[5], [5], [5], [5], [5]]
>>>
Posted by: Guest on March-10-2022

Code answers related to "python [a]*b means [a,a,...b times]"

Python Answers by Framework

Browse Popular Code Answers by Language