2

I need an explanation about the functioning of Python related to this specific case: I have a list comprehension that calls a function for every element of the listA. With this code:

listA = [1,1,1]

def operation(n):
    result = []
    *code do something*
    result = [a,b,c]
    return result 

listB = [operation(element) for element in listA]

I obtain:

listB = [[a,b,c],[a,b,c],[a,b,c]]

How can I return multiple element of a list individually so as to achieve this?

listB = [a,b,c,a,b,c,a,b,c]

What I tried is:

ListA = [1,1,1]

def operation(n):
    result = []
    *code do something*
    result = [a,b,c]
    for x in result:
        return x

Output: [a,a,a] instead of [a,b,c,a,b,c,a,b,c]

Note1: cannot use any import

Note2: I put a simplistic case and I cannot transform the list in flat list, what I would need is to be able to return multiple values ​​from a single call, is it possible? Thank you

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Alessio
  • 53
  • 3
  • 1
    See also https://stackoverflow.com/questions/354883/how-do-i-return-multiple-values-from-a-function – Brian McCutchon Dec 13 '20 at 20:53
  • 1
    Does this answer your question? [How to make a flat list out of list of lists?](https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists) – wjandrea Dec 13 '20 at 21:02

5 Answers5

5

You can iterate on the items of each returned list to flatten the output:

listB = [item for element in listA for item in operation(element)]

Note that the order of the for expressions is the same as the one you would have if you wrote 'normal' loops.

Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
1

Try this:

listA = [1, 1, 1]

def operation(n):
    result = [i for i in ["a", "b", "c"] * len(listA) for x in i]
    return result

listB = operation(listA)
print(listB)

we provide a flatten list when we make a nested for-loop.

The.B
  • 361
  • 2
  • 11
0

The following should work (for this specific structure of your list, not recommended for some other structures):

listB = sum([operation(element) for element in listA], [])
IoaTzimas
  • 10,538
  • 2
  • 13
  • 30
  • 2
    See explanation in 3rd answer here: https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists – IoaTzimas Dec 13 '20 at 21:04
  • That's the answer with this note from the author: "This is inefficient". But with the strange restriction from this question "cannot use any import" it's a way do achieve the desired output. – Matthias Dec 13 '20 at 21:12
  • I say something similar in my answer too. OP is looking for solutions, not for the best ones only – IoaTzimas Dec 13 '20 at 21:13
0

You could use your function as a generator with a secondary loop through the elements and then iterate over the generator to get your list.

listA = [1,1,1]

def operation(n):
    result = []
    ...
    result = ['a','b','c']
    
    ############
    for i in result:
        yield i      #change this to yield
    ############
    
[j for i in listA for j in operation(i)]
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51
0

if you just want to achieve your final output one way of doing it can be by using extend function like this

for i in listA:
    listB.extend(operation(listA))

this will also generate the same result but it's not a list comprehension

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Pratik Agrawal
  • 405
  • 3
  • 17