NEW_PYTHON_LEARNER
NEW_PYTHON_LEARNER

Reputation: 17

How do I get the first values in list of list as separate list?

How do I get the first values in a list of list as separate list??

For example:

A =  [ [[1,4.9], [2,90],[3,8]], [[2,34],[4,78],[9,10]], [[1,90],[3,100]] ]

the result should be as:

B = [ [1,2,3],[2,4,9],[1,3] ]

Upvotes: 0

Views: 70

Answers (3)

blairforce1
blairforce1

Reputation: 7

def getFirstValues(A):
    B = list()
    for eachListofList in A:
        currentList= list()
        for eachList in eachListofList:
            currentList.append(eachList[0])
        B.append(currentList)
     return B

Upvotes: 0

Pranav Raj
Pranav Raj

Reputation: 873

[map(None,*k)[0] for k in A]
[(1,2,3),(2,4,9),(1,3)]

Upvotes: 1

inspectorG4dget
inspectorG4dget

Reputation: 113915

In [99]: A =  [ [[1,4.9], [2,90],[3,8]], [[2,34],[4,78],[9,10]], [[1,90],[3,100]] ]

In [100]: [[item[0] for item in subl] for subl in A]
Out[100]: [[1, 2, 3], [2, 4, 9], [1, 3]]

Upvotes: 2

Related Questions