Ahsan Naseem
Ahsan Naseem

Reputation: 1106

Unpacking a list of lists

I am extracting a dictionary that's giving me this output:

mylist= [[u'Ann', u'jOhn', u'Clive'], [u'124street', u'32B', u'16eve', u'beach']]

When I try to separate it into two, I get a ValueError:

nest1, nest2 = zip(*mylist)

ValueError: too many values to unpack

Ultimately I need something like this:

nest1=['Ann', 'jOhn', 'Clive']
nest2=['124Street', '32B', '16eve', 'beach]

I found zip(*mylist) within this answer.

Upvotes: 0

Views: 1137

Answers (2)

Expert Ngobeni
Expert Ngobeni

Reputation: 81

Try this

mylist= [[u'Ann', u'jOhn', u'Clive'], [u'124street', u'32B', u'16eve', u'beach']]
nest1 = mylist[0]
nest2 = mylist[1]
print("nest1={}".format(nest1))
print("nest2={}".format(nest2))

Outputs:

nest1=['Ann', 'jOhn', 'Clive']
nest2=['124street', '32B', '16eve', 'beach']

Upvotes: 0

cs95
cs95

Reputation: 402353

*zip is meant to be used to unpack lists of tuples. In your case, there is no unpacking needed to be done, so just unpack the list itself:

In [473]: x, y = [[u'Ann', u'jOhn', u'Clive'], [u'124street', u'32B', u'16eve', u'beach']]

In [474]: x
Out[474]: ['Ann', 'jOhn', 'Clive']

In [475]: y
Out[475]: ['124street', '32B', '16eve', 'beach']

Upvotes: 5

Related Questions