-1

Code:

print([some_data[name]['indices'] for name in some_data.keys()])

Output:

[[[0, 0], [1, 0], [2, 0], [2, 1]], [[3, 0], [3, 1], [1, 1], [0, 1]], ...]

Desired Output

[[0, 0], [1, 0], [2, 0], [2, 1], [3, 0], [3, 1], [1, 1], [0, 1], ...]

Trying this method told me that the 'list' object has no attribute 'result': One liner for extend loop python

Is there an alteration that I can apply to my code to obtain a one-line solution?

Thanks in advance

Sterling Butters
  • 1,024
  • 3
  • 20
  • 41

4 Answers4

1
  topList = [[1,2],[3,4]]
  flatList = [item for subList in topList for item in subList]

see How to make a flat list out of list of lists?

AndrewStone
  • 517
  • 4
  • 14
0

You are getting tge list correctly as single list. The print() adds the extra []. Print adds to objects an object type.

0

To whoever just deleted their answer using itertools.chain - that worked and I was going to accept your answer. The final code:

list(itertools.chain(*[some_data[name]['indices'] for name in some_data.keys()]))

EDIT: Decided to go without the itertools in @Rocky Li's answer:

[e for name in some_data.keys() for e in some_data[name]['indices']]
Sterling Butters
  • 1,024
  • 3
  • 20
  • 41
0

Is this what you need? E.g. given:

some_data = {'a': {'indices': [[0, 0], [1, 1]]}, 'b': {'indices': [[2, 2], [3, 3]]}}

You want the sublists, right? So we can do:

[j for i in some_data for j in some_data[i]['indices']]

which returns:

[[0, 0], [1, 1], [2, 2], [3, 3]]
Nobilis
  • 7,310
  • 1
  • 33
  • 67