Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get inner-most elements from triple nested list Python

I have list

players = [[['QB1',7000,20],['RB1',4500,12],['RB2',3800,11]], [['QB1',7000,20],['RB2',3800,11],['RB1',4500,12]]]

How do I get the first element of each inner-most lists ('QB1', 'RB1' and 'RB2' from the first "secondary," if you will, list) to check if they are the same, however disordered labels as those in another secondary list (they are in this case as both secondary lists contain 'QB1', 'RB1' and 'RB2')?

EDIT:

My desired out is [['QB1','RB1','RB2'],['QB1','RB2','RB1']]. I want to have some way of identifying that these are, for my purpose, the same list.

like image 688
aaron Avatar asked Aug 01 '18 03:08

aaron


2 Answers

You can do this:

output = [[i[0] for i in a] for a in players]

The output will be like this:

[['QB1', 'RB1', 'RB2'], ['QB1', 'RB2', 'RB1']]
like image 50
Mehrdad Pedramfar Avatar answered Oct 03 '22 00:10

Mehrdad Pedramfar


you can use recursive search for that and get first element of each list or whole list

players = [[['QB1',7000,20],['RB1',4500,12],['RB2',3800,11]],[['QB1',7000,20],['RB2',3800,11],['RB1',4500,12]]]
def retrive_first(lst):
    for item in lst:
        if type(item) == list:
            retrive_first(item)
        else:
            print "returning ---> ", lst[0]
            return lst[0]

print retrive_first(players)
like image 38
Pankaj78691 Avatar answered Oct 02 '22 23:10

Pankaj78691