Python Forum
List structure lost when multiplying
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List structure lost when multiplying
#1
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)
Reply
#2
bd=[(0,0,0),(255,255,255)]
hg=[bd[0]]
print(hg*2)
Professional Dentist(32years) fell in love with Python during COVID-19 Lockdown.

"Nothing can stop you from learning new things except your own will"

Reply
#3
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Lost Control over VLC jrockow 8 1,072 Jul-18-2023, 06:04 PM
Last Post: jrockow
  Lost Modules standenman 2 722 Jun-22-2023, 12:18 PM
Last Post: standenman
  Lost Module standenman 10 2,834 Oct-30-2021, 05:11 PM
Last Post: deanhystad
  XML Editing formatting lost ateestructural 2 1,904 Apr-08-2021, 04:41 AM
Last Post: ndc85430
  Multiplying between two values doug2019 7 2,806 Oct-20-2020, 05:49 PM
Last Post: doug2019
  Transform list or set regardless of nesting structure blubb 2 1,955 Mar-10-2020, 07:17 PM
Last Post: ibreeden
  multiplying elements in a list Olavv 3 3,413 Feb-27-2020, 04:55 PM
Last Post: DeaD_EyE
  Most efficient way of reshaping a list-array structure midarq 2 2,062 Sep-25-2019, 09:32 AM
Last Post: midarq
  lost dictionary jjpy 1 1,883 Jul-10-2019, 12:19 PM
Last Post: scidam
  Multiplying two lists to make an array pberrett 15 5,798 May-15-2019, 02:22 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020