• Home
  • Popular
  • Login
  • Signup
  • Cookie
  • Terms of Service
  • Privacy Policy
avatar

Posted by @arijit87


10 Jul, 2024

Updated at 27 Dec, 2024

How would you use Python to fetch every 10th item from a list?

The easiest way to fetch every 10th item from a list is with a technique called ?slicing?. Here?s an example of how you do it:?

original_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

# Fetch every 10th item using slice notation

every_10th_item = original_list[::10]

print(every_10th_item)

This example would return 0, 10, and 20. If you want to start from a different number, you can modify the every_10th_item = original_list[::10] line.?

every_10th_item_starting_from_index_2 = original_list[2::10]

print(every_10th_item_starting_from_index_2)

This example would return 2, 12.

Remember that Python is a zero-based language, which means that the first element is at index 0.?