Skip to main content
fix: The correct equivalent is enumerate, since the question asks about every 10th element (index), not every element that is a multiple 10 (value).
Source Link
Mateen Ulhaq
  • 28.1k
  • 22
  • 122
  • 157
>>> xs = list(range(165))
>>> xs[0::10]
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160]

Note that this is around 100 times faster than looping and checking a modulus for each element:

$ python -m timeit -s "xs = list(range(1000))" "[x for i, x in enumerate(xs) if xi % 10 == 0]"
1000500 loops, best of 35: 525476 usec per loop

$ python -m timeit -s "xs = list(range(1000))" "xs[0::10]"
100000 loops, best of 35: 43.0232 usec per loop
>>> xs = list(range(165))
>>> xs[0::10]
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160]

Note that this is around 100 times faster than looping and checking a modulus for each element:

$ python -m timeit -s "xs = list(range(1000))" "[x for x in xs if x % 10 == 0]"
1000 loops, best of 3: 525 usec per loop

$ python -m timeit -s "xs = list(range(1000))" "xs[0::10]"
100000 loops, best of 3: 4.02 usec per loop
>>> xs = list(range(165))
>>> xs[0::10]
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160]

Note that this is around 100 times faster than looping and checking a modulus for each element:

$ python -m timeit -s "xs = list(range(1000))" "[x for i, x in enumerate(xs) if i % 10 == 0]"
500 loops, best of 5: 476 usec per loop

$ python -m timeit -s "xs = list(range(1000))" "xs[0::10]"
100000 loops, best of 5: 3.32 usec per loop
Use Haskell-like "xs" notation to signify list. https://stackoverflow.com/q/6267735/365102 . l (original) -> lst (previous editor) -> xs (best, fits with the x, too).
Source Link
Mateen Ulhaq
  • 28.1k
  • 22
  • 122
  • 157
>>> lstxs = list(range(165))
>>> lst[0xs[0::10]
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160]

Note that this is around 100 times faster than looping and checking a modulus for each element:

$ python -m timeit -s "lst"xs = list(range(1000))" "lst1 = [x"[x for x in lstxs if x % 10 == 0]"
1000 loops, best of 3: 525 usec per loop 

$ python -m timeit -s "lst"xs = list(range(1000))" "lst1 = lst[0"xs[0::10]"
100000 loops, best of 3: 4.02 usec per loop
>>> lst = list(range(165))
>>> lst[0::10]
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160]

Note that this is around 100 times faster than looping and checking a modulus for each element:

$ python -m timeit -s "lst = list(range(1000))" "lst1 = [x for x in lst if x % 10 == 0]"
1000 loops, best of 3: 525 usec per loop
$ python -m timeit -s "lst = list(range(1000))" "lst1 = lst[0::10]"
100000 loops, best of 3: 4.02 usec per loop
>>> xs = list(range(165))
>>> xs[0::10]
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160]

Note that this is around 100 times faster than looping and checking a modulus for each element:

$ python -m timeit -s "xs = list(range(1000))" "[x for x in xs if x % 10 == 0]"
1000 loops, best of 3: 525 usec per loop 

$ python -m timeit -s "xs = list(range(1000))" "xs[0::10]"
100000 loops, best of 3: 4.02 usec per loop
Tweaked variable name to comply with https://www.python.org/dev/peps/pep-0008/#names-to-avoid, which specifically lists l as one of 3 banned variable names
Source Link
Mark Amery
  • 159.2k
  • 95
  • 437
  • 478
>>> llst = list(range(165))
>>> l[0lst[0::10]
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160]

Note that this is around 100 times faster than looping and checking a modulus for each element:

$ python -m timeit -s "l"lst = list(range(1000))" "l1"lst1 = [x for x in llst if x % 10 == 0]"
1000 loops, best of 3: 525 usec per loop
$ python -m timeit -s "l"lst = list(range(1000))" "l1"lst1 = l[0lst[0::10]"
100000 loops, best of 3: 4.02 usec per loop
>>> l = list(range(165))
>>> l[0::10]
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160]

Note that this is around 100 times faster than looping and checking a modulus for each element:

$ python -m timeit -s "l = list(range(1000))" "l1 = [x for x in l if x % 10 == 0]"
1000 loops, best of 3: 525 usec per loop
$ python -m timeit -s "l = list(range(1000))" "l1 = l[0::10]"
100000 loops, best of 3: 4.02 usec per loop
>>> lst = list(range(165))
>>> lst[0::10]
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160]

Note that this is around 100 times faster than looping and checking a modulus for each element:

$ python -m timeit -s "lst = list(range(1000))" "lst1 = [x for x in lst if x % 10 == 0]"
1000 loops, best of 3: 525 usec per loop
$ python -m timeit -s "lst = list(range(1000))" "lst1 = lst[0::10]"
100000 loops, best of 3: 4.02 usec per loop
Tweaked to be python3-compatible
Source Link
Mark Amery
  • 159.2k
  • 95
  • 437
  • 478
Loading
added 327 characters in body
Source Link
Ned Deily
  • 85.5k
  • 17
  • 134
  • 156
Loading
cleaned up example
Source Link
Georg Schölly
  • 126.6k
  • 54
  • 225
  • 277
Loading
added 61 characters in body
Source Link
Ned Deily
  • 85.5k
  • 17
  • 134
  • 156
Loading
Source Link
Ned Deily
  • 85.5k
  • 17
  • 134
  • 156
Loading