Python: Remove every first element in a 2D-List

Viewed 30

i have list in python. For example Test=[[1,1],[1,2],[1,3],[1,4]]. Now i would like to create a 1D-List by removing every first number to get this: [1,2,3,4].

My current Code works just fine, however it is definitely not the most pythonic code. Could anyone give me a better code for the following? Perhaps a small explenation would be great, as i would like to understand how to programm in good pythonic code. :)


i=len(Test)
b=[]
a=0
for x in range (100):
        Test[a].remove(Test[a][0])
        b+=Test[a]
        a+=1
print(b)

greeting, Dominik

2 Answers

Using a list comprehension:

test = [[1, 1], [1, 2], [1, 3], [1, 4]]
output = [x[1] for x in test]
print(output)  # [1, 2, 3, 4]
Test=[[1,1],[1,2],[1,3],[1,4]]
# Loop through list using list comprehension and select the second 
# item
Test2 = [i[1] for i in Test]
print(Test2)
Related