buq333
buq333

Reputation: 47

python lists in a list

I have a simple question. I know how to bring out(I don't know how to say besides 'bring out') from a list. for example,

Alist = [ 1, 2, 3, 4 ]

Then,

Alist[0] = 1
Alist[1] = 2

But, what if

Blist = [[1, 2, 3 ,4], [5, 6, 7]]
Blist[0] = [1, 2, 3, 4]
Blist[1] = [5, 6, 7]

I can 'bring out' the whole [5,6,7] as calling Blists[1]

My question is, how to bring out specific number in the list, lets say number 5. Hope this makes sense

Upvotes: 0

Views: 67

Answers (2)

Ryan Haining
Ryan Haining

Reputation: 36792

You know that B[1] gets you a reference to the second list in B.

lst = B[1]

You can index that result again to get another element

lst[0]

However you can of course do this more easily in one line

B[1][0]

Upvotes: 3

Prune
Prune

Reputation: 77837

Blist[1] is a list itself. To get the head element, use an index on Blist[1]

Blist[1][0]

Upvotes: 2

Related Questions