0

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

0

1 Answer 1

2

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)].

2
  • 2
    [list(x) for x in zip(dev,phone,carrier)] to get a lists of lists Commented Oct 15, 2014 at 22:58
  • Thanks @PadraicCunningham. Your method also works as requested for Python 3 (where zip returns an iterator). Updated my answer. Commented Oct 15, 2014 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.