d.raev
d.raev

Reputation: 9546

Generate list, two components at a time

I want to generate an list of list, where one of the elements is consecutive numbers, and the other is all elements from a short, predefined set:

// Needed result:
[['A', 1], ['B', 1],['A', 2], ['B', 2], ['A', 3], ['B', 3]....]

I tried some variations of the list generation, but can not find a working solution:

[['A', x],['B', x] for x in range(1,100)]

[[key, x] for key in ['A', 'B'] for x in range(1,100)]

Upvotes: 0

Views: 73

Answers (2)

ABHINAV SHARMA
ABHINAV SHARMA

Reputation: 41

Do it the other way.

[[key, x] for x in range(1,100) for key in ['A','B']]

Upvotes: 1

maciejwww
maciejwww

Reputation: 1196

List comprenhesion should do it in one line:

new_list = [[key, i] for i in range(1,100) for key in your_set]

Upvotes: 1

Related Questions