Python Forum

Full Version: List structure lost when multiplying
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Why I get different format of results when I am multiplying one item of the list and the whole list?


I use triplets of numbers for RGB color coding.
When I multiply the whole list I get nicely multiplied list:

bd=[(0,0,0),(255,255,255)]
hg=bd*2
print(hg)
Output:
[(0, 0, 0), (255, 255, 255), (0, 0, 0), (255, 255, 255)]
But when I multiply just one item from the list
bd=[(0,0,0),(255,255,255)]
hg=bd[0]*2
print(hg)
suddenly triplets are gone and I get all output in one long list:
Output:
(0, 0, 0, 0, 0, 0)
bd=[(0,0,0),(255,255,255)]
hg=[bd[0]]
print(hg*2)
(Apr-23-2020, 02:44 AM)Protonn Wrote: [ -> ]Why I get different format of results when I am multiplying one item of the list and the whole list?
the result is the same. some_list * 2 will return list with elements of the original list repeated twice. In first case elements are of type tuple, in the second elements are of type int.