Python: Dump whole list in a variable

Question:

I have this code:

def write_extension(self):

    extension = []
    dtd = ""
    if extension == []:
            extension = extension.extend(self.edids[]  (Thanks to Howardyan for making it clearer for me)

    print(extension)

I want to write the complete array in the new array extension but i dont know how. Is there an easy way doing this? Later on (when extension is not empty) i just want to add the array to the end of the extension array.

To Make it clear. Now i have this:

  • Name1 = [1, 2, 3]
  • Name2 = [4, 5, 6]
  • Name3 = [7, 8, 9]

And i want to have this after i added all arrays to extension

extension = [123, 456, 789]

Asked By: Nico

||

Answers:

extension’s type is str. cannot add with a list.
you need do this:

def write_extension(self):

    extension = []
    dtd = ""
    if extension == []:
            extension = extension.extend(self.edids[:])
            print(extension)
Answered By: Howardyan

You can join all the elements in your individual lists, and then extend from extension like so:

name1 = [1, 2, 3]
name2 = [4, 5, 6]
name3 = [7, 8, 9]
extension = []
    
''.join(map(str, name1))
''.join(map(str, name2))
''.join(map(str, name3))

extension.extend((name1, name2, name3))

>>> [123, 456, 789]
Answered By: Phillip
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.