Reputation: 17
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
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
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