Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8327909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:16:18+00:00 2026-06-09T01:16:18+00:00

For every run of x or more consecutive zeros in a list in Python,

  • 0

For every run of x or more consecutive zeros in a list in Python, I would like to del all zeros in the run except for x of them. If x = 0, then delete all zeros.

I was thinking of a Python function that took a list, L, and a number, x, as inputs.

For example, let L = [7, 0, 12, 0, 0, 2, 0, 0, 0, 27, 10, 0, 0, 0, 0, 8].

  • If x = 0, then return L = [7, 12, 2, 27, 10, 8]
  • If x = 1, then return L = [7, 0, 12, 0, 2, 0, 27, 10, 0, 8]
  • If x = 2, then return L = [7, 0, 12, 0, 0, 2, 0, 0, 27, 10, 0, 0, 8]
  • If x = 3, then return L = [7, 0, 12, 0, 0, 2, 0, 0, 0, 27, 10, 0, 0, 0, 8]
  • If x = 4, then return L = [7, 0, 12, 0, 0, 2, 0, 0, 0, 27, 10, 0, 0, 0, 0, 8] (Same as original L)
  • If x >= 5, then return original L as there are no runs of 5 or more consecutive zeros.

Any help would be sincerely appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T01:16:19+00:00Added an answer on June 9, 2026 at 1:16 am

    This is easy to do as a generator. Wrap your call to it in a list constructor if you want a fresh list with the zero-runs removed.

    def compact_zero_runs(iterable, max_zeros):
        zeros = 0
        for i in iterable:
            if i == 0:
                zeros += 1
                if zeros <= max_zeros:
                    yield i
            else:
                zeros = 0
                yield i
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program which I would like to run every X min the
I'm trying to schedule a repeating event to run every minute in Python 3.
I have an ajax app that will run functions on every interaction. I'd like
I have a long-running process that must run every five minutes, but more than
i currently have a list of classes every class starts a scheduler like this:
I am trying to get a timer run every minute in sync with the
If I have a cron job that run every 10 minutes and for some
I have a PHP page that I run every minute through a CRON job.
I have a php script I need to run every 5 seconds (run, wait
I have a Perl script which I want to run every 4 hours through

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.