Home>
I want to know

I want to add another list to the list, but if I do something like below, [['A','B','C']] It will not be possible to take out.
I want to say ['A','B','C'] What should I do?

list = []
output = []
list.append ("A")
list.append ("B")
list.append ('C')
output.append (list)
print (output)


In addition, I think that it was written in the book that variable declaration is not necessary in python,

list.append ("A")


I created a variable called list, and I thought that "A" was in it, but I got an error.
So

list = []


I am doing this because it is fixed when I do something like a declaration first, but is this correct?

Additional
Excuse me. ['A','B','C'] could be retrieved with output [0].
I want to tell you whether output [0] can extract'A&apos ;.

  • Answer # 1

      

    ['A','B','C']

    Are you going to add lists?
    In such cases, use list # extend instead of list # append.

    >>>lst = list ('ABC')
    >>>lst
    ['A', 'B', 'C']
    >>>
    >>>output = []
    >>>
    >>>output.extend (lst)
    >>>output
    ['A', 'B', 'C']
    >>>
    >>>output + = lst # This is fine
    >>>output
    ['A', 'B', 'C', 'A', 'B', 'C']
      

    I think it was written in the book that python doesn't need variable declarations

    Variable names and objects are only dynamically associated with assignments.


    Please do not name variables asliststrint.
    Causes errors that are difficult to resolve.

    >>>list ('ABC')
    ['A', 'B', 'C']
    >>>
    >>>list = [1, 2, 3]
    >>>
    >>>list ('ABC')
    Traceback (most recent call last):
      File "<stdin>", line 1, in<module>
    TypeError: 'list' object is not callable

  • Answer # 2

    The intended action is probably

    output.extend (list)

    Variable declaration and initialization are separate. More specifically, types don't have to be written in python.

    Because it is a dynamically typed language, there is no need to indicate that alisttype variable calledlistis used. I think you are saying.

    As a matter of fact, if you write a process that creates an empty list and adds elements, you need to create an empty list and bind it to a variable.


    Using the variable name

    listwill override the built-inlistclass. Avoid using variable names that cover other things.