Mark Kennedy
Mark Kennedy

Reputation: 1781

Python return subset of multidimensional list

Suppose I have the following Python multi dimensional list

oldList = [['a', '0', '1', '2'], ['b', '3', '4'], ['c', '5']]

How can I get each list within this multi d list?

i.e. newList1 = ['a', '0', '1', '2'] 
     newList2 = ['b', '3', '4']
     newList3 = ['c', '5']

Upvotes: 0

Views: 230

Answers (3)

roman
roman

Reputation: 117400

You can use values unpacking:

oldList = [['a', '0', '1', '2'], ['b', '3', '4'], ['c', '5']]

newList1, newlist2, newlist3 = oldList

Upvotes: 4

Brionius
Brionius

Reputation: 14098

You can use use indexing with oldList:

newList1 = oldList[0]
newList2 = oldList[1]
newList3 = oldList[2]

Upvotes: 1

user2357112
user2357112

Reputation: 280973

You already have each sublist. They're in the list. If you want the first one, you could use

oldList[0]

or if you know there will be 3 sublists and you want all of them, you could use

a, b, c = oldList

or if you want to iterate over them, you could use

for sublist in oldList:
    do_stuff_with(sublist)

Iteration is probably what you want.

Upvotes: 5

Related Questions