• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Keep Every Nth Element in List in Python

Keep Every Nth Element in List in Python

May 6, 2022 Leave a Comment

To keep every nth element in a list in Python, the easiest way is to use slicing.

lst = [1, 2, 3, 4, 5, 6, 7]

every_3rd = lst[::3]

print(every_3rd)

#Output:
[1, 4, 7]

If you want to create a function which will keep every nth element in a list, you can do the following:

def keep_every_nth(lst, n):
    return lst[::n]

example = [1, 2, 3, 4, 5, 6, 7]

print(keep_every_nth(example,3))

#Output:
[1, 4, 7]

When working with collections of data, the ability to easily keep or remove specific items from a collection can be valuable.

One such operation in Python which is common is keeping every nth element in a list.

To keep every nth element of a list in Python, you can use slicing and pass n for the step size.

For example, if you have a list and you want every 2nd element, starting with the first element, you would get the slice defined by [::2] as shown below.

lst = [1, 2, 3, 4, 5, 6, 7]

every_2nd = lst[::2]

print(every_2nd)

#Output:
[1, 3, 5, 7]

If you want to create a function which will keep every nth element in a list given a list and n, you can use the following:

def keep_every_nth(lst, n):
    return lst[::n]

example = [1, 2, 3, 4, 5, 6, 7]

print(keep_every_nth(example,3))

#Output:
[1, 4, 7]

If you want to start with a different element, then you can adjust the starting point of your slice.

Removing Every Nth Element from a List in Python

If you want to go the other way and remove every nth element from a list in your Python code, you can also use slicing.

In this case, we will need to adjust the slice we take and also utilize the Python del keyword to delete elements from the list.

Below is a function which will remove every nth element from a list given a list and n in Python.

def remove_every_nth(lst, n):
    del lst[n-1::n]
    return lst

example = [1, 2, 3, 4, 5, 6, 7]

print(remove_every_nth(example,3))

#Output:
[1, 2, 4, 5, 7]

Hopefully this article has been useful for you to learn how to keep every nth element in a list in Python.

Other Articles You'll Also Like:

  • 1.  Drop First Row of pandas DataFrame
  • 2.  Unset Environment Variables in Python
  • 3.  Using Python to Check if String Contains Only Letters
  • 4.  How to Serialize a Model Object with a List of Lists Using Django Rest Framework in Python
  • 5.  islower Python – Check if All Letters in String Are Lowercase
  • 6.  Convert String to List Using Python
  • 7.  Python ljust Function – Left Justify String Variable
  • 8.  pandas cumprod – Find Cumulative Product of Series or DataFrame
  • 9.  Get Days of timedelta Object in Python
  • 10.  Count Spaces in String in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy