0

I am able to get it to work but I don't understand how it works

li=[[0,1,2],[3,4,5],[6,7,8]]
li2 = [ y for x in li for y in x]

output: li2: [0,1,2,3,4,5,6,7,8]

new user
  • 31
  • 4
  • 3
    Does this answer your question? [Explanation of how nested list comprehension works?](https://stackoverflow.com/questions/20639180/explanation-of-how-nested-list-comprehension-works) – python_user Jan 31 '21 at 07:25
  • `[elem for sublist in li for elem in sublist ]` – Ajay Jan 31 '21 at 07:26

1 Answers1

0

Second line can be written as:

li2 = []
for x in li:
    for y in x:
        li2.append(y)
Michael Butscher
  • 10,028
  • 4
  • 24
  • 25