0

I search something in python to return a result from a list every 4 number

Here an example:


list=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]

I would like only [1,5,9,13,17]

So every 4 elements of list It returns me the result !

So I think I need use probably the operator % but I don't know how I can do it.

Thanks !!

Pierre
  • 41
  • 5
  • 2
    Does this answer your question? [Pythonic way to return list of every nth item in a larger list](https://stackoverflow.com/questions/1403674/pythonic-way-to-return-list-of-every-nth-item-in-a-larger-list) – Helios Apr 08 '20 at 19:40
  • 1
    `list[::4]` - iterate through every fourth element in the list. Also `list` is a keyword, so don't use it as a variable name – rdas Apr 08 '20 at 19:40

2 Answers2

1

I would use:

list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]
list[::4]
0

try this one:

my_list=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]
print(my_list[0::4])

and list is not a good name for your variable since it's a builtin function in python.

Gabio
  • 9,126
  • 3
  • 12
  • 32