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 6783057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:49:09+00:00 2026-05-26T16:49:09+00:00

I was wondering, if there is way in Python to modify collections without creating

  • 0

I was wondering, if there is way in Python to modify collections without creating new ones. E.g.:

lst = [1, 2, 3, 4, 5, 6]
new_lst = [i for i in lst if i > 3]

Works just fine, but a new collection is created. Is there a reason, that Python collections lack a filter() method (or similar) that would modify the collection object in place?

  • 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-05-26T16:49:09+00:00Added an answer on May 26, 2026 at 4:49 pm

    The other answers are correct; if you want all the names pointing to the old list to point to the new list you can use slice assignment.

    However, that’s not truly in-place creation; the new list is first created elsewhere. The link in Sven’s answer is good.

    The reason there isn’t one that truly operates in-place is that while making a new list like that is O(n), each truly in-place item removal would be O(k) by itself, where k is the length of the list from the removal point on. The only way to avoid that with Python lists is to use some temporary storage, which is what you’re doing by using slice assignment.

    An example of an in-place O(n) filter on a collections.deque, in case you don’t need to store your data in a list:

    from collections import deque
    
    def dequefilter(deck, condition):
        for _ in xrange(len(deck)):
            item = deck.popleft()
            if condition(item):
                deck.append(item)
    
    deck = deque((1, 2, 3, 4, 5))
    dequefilter(deck, lambda x: x > 2) # or operator.gt(2)
    print deck
    # deque([3, 4, 5])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Python 2.x and I'm wondering if there's a way to tell if
Just wondering if there is any way (in C) to get the contents of
Just wondering if there is any way to get the NS records in C#.
Just wondering if there is a way in Spring to have a parent controller:
I was wondering if there's a way in Python(2.6) to get only the name
I was wondering if there's a way to plot a data cube in Python.
Is there a way to tell Python about additional site-packages locations without modifying existing
I am wondering whether there's a lightweight way to distribute python tasks among different
Hello Python experts, I am wondering if there is any shorter way to assign
I was wondering if there is any way to export python functions to dll.

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.