Reputation: 776
So I think it is much easier to explain if I show an example:
things = ["black", 7, "red', 10, "white", 15]
two new lists based on whether the index was even or odd.
color = ["black", "red","white"]
size = [7,10,15]
Upvotes: 0
Views: 47
Reputation: 3207
things = ["black", 7, "red", 10, "white", 15]
two_lists = zip(*[(x,things[things.index(x)+1]) for x in things[::2]])
>>> two_lists[0]
('black', 'red', 'white')
>>> two_lists[1]
(7, 10, 15)
the list manip part [(x,things[things.index(x)+1]) for x in things[::2]]
seperates it into 3 list pairs ( almost like its ready for a dictionary or something... so cray cray)
the zip(*
part, turns the whole array on its side
if you just did [(x,things[things.index(x)+1]) for x in things[::2]]
withotu the zip part, then you could call dict()
on it and return a dictionary, which might be more useful
>>> a = [(x,things[things.index(x)+1]) for x in things[::2]]
>>> a
[('black', 7), ('red', 10), ('white', 15)]
>>> cool_dict = dict(a)
>>> cool_dict['black']
7
Upvotes: 0
Reputation: 113915
In [4]: things = ["black", 7, "red", 10, "white", 15]
In [5]: color = things[::2]
In [6]: color
Out[6]: ['black', 'red', 'white']
In [7]: size = things[1::2]
In [8]: size
Out[8]: [7, 10, 15]
Upvotes: 8