0

I want to make a list like this:

[str(s),'Numpad{}'.format(s) for s in range(0,10)]

that produces:

['1' , 'Numpad1' , '2' , ... ]

Is there a syntactically allowable way to do this?

Alex
  • 1,172
  • 11
  • 31

2 Answers2

1

I found a great solution to you question here on a question regarding flatting of lists.

import itertools
list(itertools.chain(*[[str(s),'Numpad{}'.format(s)] for s in range(1,10)]))

Here more elegant solution provided by tdelaney in the comments section:

list(itertools.chain.from_iterable([str(s),'Numpad{}'.format‌​(s)] for s in range(0,10)))
Community
  • 1
  • 1
tommy.carstensen
  • 8,962
  • 15
  • 65
  • 108
0

Use a tuple?

In [1]: [(str(s),'Numpad{}'.format(s)) for s in range(0,10)]
Out[1]:
[('0', 'Numpad0'),
 ('1', 'Numpad1'),
 ('2', 'Numpad2'),
 ('3', 'Numpad3'),
 ('4', 'Numpad4'),
 ('5', 'Numpad5'),
 ('6', 'Numpad6'),
 ('7', 'Numpad7'),
 ('8', 'Numpad8'),
 ('9', 'Numpad9')]

Iterate over that with

for (val, numpad) in lst:
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Not really what I want since I don't want to search for tuples – Alex Oct 06 '16 at 03:15
  • Well, you didn't say anything about searching – OneCricketeer Oct 06 '16 at 03:16
  • 1
    @AlexEshoo I think you should provide more information in your question on what you are trying to achieve so readers can know how to give you a good solution with *all* the necessary information. – idjaw Oct 06 '16 at 03:17