Reputation: 162
I have been trying to resolve this issue.
I have 3 lists
dev = ['Alex', 'Ashley', 'Colin']
phone = ['iPhone', 'Nexus', 'Nokia']
carrier = ['ATT', 'T-Mobile', 'MegaFon']
Is this a pythonic way to create a new 2D array like this:
matrix = [['Alex', 'iPhone', 'ATT'], ['Ashley','Nexus','T-Mobile'], ['Colin', 'Nokia', 'MegaFon']]
Thank you
Upvotes: 0
Views: 176
Reputation: 96111
I think you want:
>>> zip(dev, phone, carrier)
[('Alex', 'iPhone', 'ATT'),
('Ashley', 'Nexus', 'T-Mobile'),
('Colin', 'Nokia', 'MegaFon')]
See zip
documentation.
If you need a list of lists (instead of a list of tuples in Python 2, or an iterator in Python 3), use Padraic's suggestion: [list(x) for x in zip(dev, phone, carrier)]
.
Upvotes: 2