0

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

Dominik
  • 23
  • 5
  • Why are you looping until 100? Where did this value come from? – TDG Sep 23 '22 at 07:54
  • @TDG Ah sorry, that was supposed to be i. 100 was the length of the List. However it is clear that i is the same. Forgot to change that – Dominik Sep 23 '22 at 07:59
  • The shown output doesn't really match the explained logic (if you remove the first item this should rather give `[[1], [2], [3], [4]]`, using `[x[1:] for x in test]`) – mozway Sep 23 '22 at 08:02

2 Answers2

0

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]
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0
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)