0

I have three lists, namely:

commands = ["ping", "exec", "someCmd"]
parameters = ["127.0.0.1", "mspaint", "someParam"]
iterations = ["2", "1", "3"]

as you can imagine, each item index correspond to same index in every list, so I want to have a list of lists where each element corresponds to each program, that is:

[['ping', '127.0.0.1', '2'], ['exec', 'mspaint', '1'], ['someCmd', 'someParam', '3']]

well, that's easy for a beginner like me with a for loop:

new_list = []
for c, _ in enumerate(commands):
    row = [commands[c], parameters[c], iterations[c]]
    new_list.append(row)

or the actually first that i came up with (i don't know if it's better or worse)

new_list = []
for c, command in enumerate(commands):
    row = [command, parameters[c], iterations[c]]
    new_list.append(row)

but I don't want to give up and tried to achieve the same with a list comprehension, which i did obviously bad, that's why I'm here

my_attempt = [[c, p, i] for c, p, i in commands, parameters, iterations]

but this surprisingly (for me) just make a list of list but every element is just like the input, that is:

[['ping', 'exec', 'asd'], ['127.0.0.1', 'paint', 'param'], ['2', '1', '3']]

Would you help me to fix this one, or suggest me other approaches?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
alete
  • 608
  • 2
  • 8
  • 23
  • You just want `list(zip(commands, parameters, iterations))` – juanpa.arrivillaga Jan 23 '18 at 21:49
  • If you wanted to actually tranform the elements, you'd do something like: `[c+p+i for c,p,i in zip(commands, parameters, iterations)]` – juanpa.arrivillaga Jan 23 '18 at 21:50
  • @juanpa.arrivillaga He's using Python 2. No need for the conversion to list. – Christian Dean Jan 23 '18 at 21:51
  • @juanpa.arrivillaga zip returns a list of tuples, adding list() around it changes nothing. In any case I have to change the inner tuples to lists, right? – alete Jan 23 '18 at 21:58
  • 1
    @alete yes, sorry, didn't see you were on Python 2. In Python 3, `zip` returns an iterator. To convert to lists, something as simple as `map(list, zip(...))` would do. – juanpa.arrivillaga Jan 23 '18 at 21:59
  • @alete: You are iterating over the tuple `(commands, parameters, iterations)`, unpacking each into three variables. Had your input lists been longer you’d have had an exception instead. – Martijn Pieters Jan 23 '18 at 22:00
  • 1
    Note that the literal transformation of your working code would have been `[[command, parameters[c], iterations[c]] for c, command in enumerate(commands)]`; moving the expression you append each iteration to the front. – Martijn Pieters Jan 23 '18 at 22:07
  • Thanks everyone. If you want to sum up everything in this comments in one answer I would be glad to mark it as accepted, since the others are poor. – alete Jan 23 '18 at 22:10
  • The question is already closed as a duplicate, sorry. – Martijn Pieters Jan 23 '18 at 22:19

2 Answers2

0
zip(commands, parameters, iterations)
Matti Lyra
  • 12,828
  • 8
  • 49
  • 67
0

what you need is the zip() function

zip(commands,parameters,iterations)

will give you the result you need

usernamenotfound
  • 1,540
  • 2
  • 11
  • 18