Skip to main content
17 events
when toggle format what by license comment
Nov 22, 2023 at 1:50 history edited Mateen Ulhaq CC BY-SA 4.0
fix: The correct equivalent is enumerate, since the question asks about every 10th element (index), not every element that is a multiple 10 (value).
Nov 22, 2023 at 1:41 history edited Mateen Ulhaq CC BY-SA 4.0
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).
Jun 20, 2023 at 5:27 comment added Michal Kovacik this is correct answer, it returns every 10th element from the list indeed, and not element divisable by 10. (python 3.9). my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] my_list[::10] Out[53]: [1, 11]
Oct 16, 2020 at 14:05 comment added user9363390 Your comparison is not appropriate because you are selecting every element that is divisible by 10 instead of selecting every 10th element. It will be more appropriate to compare slicing notation with this: lst = list(range(1000)); lst1 = [lst[i] for i in range(0, len(lst), 10)]. On my machine, I get "1000000 loops, best of 5: 395 nsec per loop" for slicing notation and "20000 loops, best of 5: 11.1 usec per loop" for list comprehension.
Nov 16, 2019 at 15:20 history edited Mark Amery CC BY-SA 4.0
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
Nov 16, 2019 at 15:14 history edited Mark Amery CC BY-SA 4.0
Tweaked to be python3-compatible
Jan 4, 2018 at 11:51 comment added Damo Im surprised by the performance comparison 0.5 seconds for list comprehension and 0.4s for list slice. Seems very slow, why list slicing needs 100 thousand loops for a list of size 1 thousand!?
Aug 5, 2016 at 11:20 comment added Konstantin Schubert The 0 is redundant in l[0::10]. l[::10] is more readable, less confusing.
Sep 10, 2009 at 8:10 comment added Ned Deily @Andre: very true. So this is an example of a humble language feature, the slice operator, which turns out in this case (1) to make it easier to get the correct results; (2) results in a more concise expression; and (3) happens to be 2 orders of magnitude faster. (1) is by far the most important concern, of course, but, thanks to the careful design and implementation of the language, you get all three for the price of 1. Nice question and responses.
Sep 10, 2009 at 7:26 comment added Andre Miller I commented on this below in the list comprehension answers. Be careful with "if x % 10 == 0". It works only with this particular list example, but if the input list is for example l=range(0,1000,2) it won't pull out every 10th item.
Sep 10, 2009 at 7:18 history edited Ned Deily CC BY-SA 2.5
added 327 characters in body
Sep 10, 2009 at 6:53 vote accept p.brown
Sep 10, 2009 at 6:53 history edited Georg Schölly CC BY-SA 2.5
cleaned up example
Sep 10, 2009 at 6:50 history edited Ned Deily CC BY-SA 2.5
added 61 characters in body
Sep 10, 2009 at 6:45 comment added Ned Deily Sure, list comprehensions are more powerful in general. OTOH, the question posits an existing list and, in that case, a slice works just fine.
Sep 10, 2009 at 6:38 vote accept p.brown
Sep 10, 2009 at 6:42
Sep 10, 2009 at 6:37 history answered Ned Deily CC BY-SA 2.5