0

I have a list of dictionaries and in each of the dictionaries, I have a property that contains a list of ids. so I need to get the content of my properties as a new list . I tried to do this by inline for loop syntax . but I get this result :

my_list  = [{"sessions": [1,2,3,42,56]}, {"sessions": [1,52,53,4,55]},{"sessions": [1,4,5]}]
listed_sessions =[ s for s in [j["sessions"] for j in my_list for item in j]]

print(listed_sessions)

result :

[[1, 2, 3, 42, 56], [1, 52, 53, 4, 55], [1, 4, 5]]     

I mixed two different for loop as you can see. but I need to know, how can I get my result in a flat list?

Babak Abadkheir
  • 2,222
  • 1
  • 19
  • 46

1 Answers1

2
listed_sessions =[j for item in my_list for j in item["sessions"]]
AlexisG
  • 2,476
  • 3
  • 11
  • 25