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?
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?
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)))
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: