-2

I have a list in python, eg. [1,2,3,4,5,6,7,8,9,10,11,12]

I want to make a new list with only the 4. element from the right? .. how to do that, eg. with a one liner.

eq. to .. [4,8,12]

pkdkk
  • 3,647
  • 8
  • 39
  • 65

2 Answers2

2

You can do it simply like so in one line: (where a is your list)

print(a[-1::-4][::-1])
Sahith Kurapati
  • 1,267
  • 6
  • 12
1
foo =  [1,2,3,4,5,6,7,8,9,10,11,12]
print(foo[-1::-4])

output

[12, 8, 4]

reverse it, if the order is important

buran
  • 11,840
  • 7
  • 28
  • 49