list in def function

Question:

I have been trying to solve this problem in def function for doing a list and return it but it seems have to use your help.

def something():
    a_list = []
    a_list.append("note")
    return a_list
def main():
    b_list = []
    c = something()
    b_list.append(c)
    print(b_list)
main()

Output:

['note'] #normally will print note only
Asked By: bob

||

Answers:

You need to use .extend instead of .append

Can you try the following:

def something():
    a_list = []
    a_list.append("note")
    return a_list

def main():
    b_list = []
    c = something()
    b_list.extend(c)
    print(b_list)

main()

Output:

['note']
Answered By: Jeril

Try using .extend instead of .append:

b_list.extend(c)
Answered By: Vladimir Fokow
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.