-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,905
  • 8
  • 44
  • 69

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,617
  • 10
  • 14
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
  • 13,682
  • 10
  • 36
  • 61