Python: Dump whole list in a variable

1.2k views Asked by At

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]

2

There are 2 answers

2
Howardyan On BEST ANSWER

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)
2
Phil On

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]