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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:52:51+00:00 2026-05-20T21:52:51+00:00

Come straight to the point. A list e.g. lst = [1, 1, 1, 0,

  • 0

Come straight to the point. A list e.g. lst = [1, 1, 1, 0, 0, 0], now I want to change 1 into 0 as lst = [0, 0, 0, 0, 0, 0].

My original solution likes below:

for item in lst:

    if item == 1:

        item = 0

Actually, the result list is still lst = [1, 1, 1, 0, 0, 0].

Q1: Is there any other solutions to solve it besides using list comprehension?

Q2: How to write correct list comprehensions when many more if else for in it? So I have tried four styles below:

No.1 lst = [0 if item == 1 for item in lst] # Returns syntax error

No.2 lst = [0 for item in lst if item == 1] # Returns [0, 0, 0]

No.3 lst = [0 if item == 1 else item for item in lst] # Returns [0, 0, 0, 0, 0, 0]

No.4 lst = [0 for item in lst if item == 1 else item] # Returns syntax error
  • 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-20T21:52:51+00:00Added an answer on May 20, 2026 at 9:52 pm

    Neither list comprehension nor for item in iterable mutate the list. In the former case, that’s (practically) impossible by design, in the latter case it’s because Python is pass by value only.

    You can use a for loop and change values at specific indices. Just don’t to delete or add anything while iterating. enumerate allows you to have your cake (the item) and eat it (have to index) too.

    for i, item in enumerate(lst):
        if item == 1:
          list[i] = 0
    

    List comprehensions in general are of the form [expression for target in expression if condition]. The optional filtering part at the end can only skip an item (think of it as if not condition: continue), choosing the item to put into the list being generates happens in the first expression – a few versions ago, conditional expressions (expression if condition else expression) were added, so you can do some basic selection in that expression. Doing this twice or more in one expression really pushes it though, so refrain from it for more complex logic.

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

Sidebar

Related Questions

I`v come across a need where I want to create multiple list items from
Straight to the point: I have this javascript: for(item=1;item<5;item++) { xmlhttp=new XMLHttpRequest(); xmlhttp.open(GET,'zzz.php', true);
How come this works: import datetime now = datetime.datetime.now() month = '%d' % now.month
Say I want to get ten records with the MOST likes in the last
I just want to play a very simple, straight forward note by giving my
Im a bit embarrased asking this, so i'll just come straight out with it.
We’re trying to come up with something approaching a simple and straight-forward model for
I've been working on a site, and come into a snag. I've got various
Come across what looks at first sight like an MT-issue, but I'm trying to
I come from a C++ background and I've been working with C# for about

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.