1

I have the following:

def fun(data):
    ...do some processing
    ret_data = ['a','b','c']
    return ret_data

l1 = []
l1.append(fun(data))

Output:

l1 is [['a','b','c']]

I don't want to create another list and unpack it to it. I want to use l1 but with the extra [] removed so that the output is:

Need:

l1=['a','b','c']

having a difficult time with this...

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
magicsword
  • 1,179
  • 3
  • 16
  • 26
  • 2
    Why not just `l1 = fun(data)`? – Carcigenicate Aug 15 '20 at 19:08
  • Does this answer your question? [What is the difference between Python's list methods append and extend?](https://stackoverflow.com/questions/252703/what-is-the-difference-between-pythons-list-methods-append-and-extend) – mkrieger1 Aug 15 '20 at 19:15

2 Answers2

4

You can use list.extend:

def fun():
    # ...do some processing
    ret_data = ['a','b','c']
    return ret_data

l1 = []
l1.extend(fun())

print(l1)

Prints:

['a', 'b', 'c']
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
3

If the code looks exactly like that and you create l1 before appending you don't need to set l1 = [] you can just set l1 to the return value of the function, like this: l1 = fun(data).

Marko Borković
  • 1,884
  • 1
  • 7
  • 22