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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:02:17+00:00 2026-06-10T07:02:17+00:00

Possible Duplicate: What is the difference between slice assignment that slices the whole list

  • 0

Possible Duplicate:
What is the difference between slice assignment that slices the whole list and direct assignment?

I don’t have money for school so I am teaching myself some Python whilst working shifts at a tollbooth on the highway (long nights with few customers). (BTW: Coursera should be translated to all languages…)

I have read here that if I have a list l:

l = ['a', '', 'b']

and I want to filter out empty strings like so:

l = [c for c in l if c]

or like so:

l = filter(lambda x: x, l)

it is advisable to do this instead:

l[:] = ... # either method 1 or method 2 above

not to “lose” the reference to the first l, especially in case other variables were pointing to it.

My question:

  • Why is it that l[:] denotes “the contents of l” in this case, allowing specifically reassignment to the “same” l, when elsewhere I think of it as a “same size slice”, conveniently creating a copy of l for me?

  • Did I misunderstand how to use the l[:] for same-list-reassignments completely?

I thought that if I had an l and I asked for a l[:], the latter was an actual copy of the original l?

Reference: “Learning Python” -> There are a variety of
ways to copy a list, including using the built-in list function and the standard library
copy module. Perhaps the most common way is to slice from start to finish

Thanks!

  • 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-10T07:02:19+00:00Added an answer on June 10, 2026 at 7:02 am

    There’s a difference in Python between getting a slice of a list and setting a slice of a list. Indeed, they are actually separate operations (__getitem__ and __setitem__, respectively). So, what’s true for the get case may not be true for set.

    In the former case, l[:] means to get a copy of the list (it generates a brand new list which has all the same contents as the old list). In the latter case, l[:] = ... means to set the contents of the list to something else.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Difference between Color.red and Color.RED I have seen that the Java class
Possible Duplicate: difference between string object and string literal Let's say I have two
Possible Duplicate: Difference between int[] array and int array[] I was sure that this
Possible Duplicate: Calculating the Difference Between Two Java Date Instances hi, I have two
Possible Duplicate: Difference between e.target and e.currentTarget I don't really understand the difference between
Possible Duplicate: Difference between object and instance I have couple of questions: Every instance
Possible Duplicate: Difference between ref and out parameters in .NET I know that ref
Possible Duplicate: Difference between (function(){})(); and function(){}(); I have seen it a lot by
Possible Duplicate: Difference between Property and Field in C# I thought that basic properties
Possible Duplicate: Difference between Category and Class Extension? I am often told that when

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.