3

What I want to do is replace all the even numbers of a list with 0 for instance

list = [1,2,3,4,5] would be list = [1,0,3,0,5]

I thought about doing it this way

list = [1,2,3,4,5]

for i in list:
  if i % 2 == 0:
  # then replace the even numbers with 0 

the problem is that I cant figure out how to write the next line of code

6 Answers 6

6

The problem is that if you start like this

for i in lst: 
    if i %2 == 0:
         # don't know the index of i

However, you can use enumerate to keep trace of the index - now it is easy

for idx, i in enumerate(lst):
    if i % 2 == 0:
        lst[idx] = 0

Usually it's simpler to use a list comprehension and replace the whole list with a new one.

You can test the lowest bit of each number using the bitwise and &

>>> L = [1, 2, 3, 4, 5] 
>>> L = [x if x & 1 else 0 for x in L]
>>> L
[1, 0, 3, 0, 5]

Depending on your CPU and the Python implementation, you may find this is faster (but less readable in my opinion):

[x * (x & 1) for x in L]
Sign up to request clarification or add additional context in comments.

2 Comments

+ one for the Best answer.
Thanks John and everyone, I am a beginner in python and this was really useful
2

You can do this with a list comprehension like so:

list = [x * (x%2) for x in list]
print list

Notice that x%2 can only result in 1 or 0. Hence, if the number is even, the result of multiplication is 0, and if it is odd, it is the number itself.

Demo

Comments

2
>>> my_list = [1,2,3,4,5]
>>> def myfun(x):
    if x % 2 != 0:
        return x
    else:
        return 0


>>> list(map(myfun, my_list))
[1, 0, 3, 0, 5]

1 Comment

I like your unique as well as easy usage of map functions.For a newbie this suits the best fit.
1

Try this:

my_list = [1,2,3,4,5]
my_result = list()

for number in my_list:
    if number % 2 == 0:
        my_result.append(0)
    else:
        my_result.append(number)

print my_result

You can also use list comprehensions:

my_list = [1,2,3,4,5]
print [0 if x%2==0 else x for x in my_list]

Output is the same for both examples:

[1, 0, 3, 0, 5]

It is important to highlight that list is a Python reserved word for its list data structure. That's why I assign the list a different name.


Documentation:

List comprehensions: https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions

2 Comments

@shree.pat18 I've modified my answer. I misunderstood what he needed. Thanks!
@RahulLakhanpal I've modified my answer. I misunderstood what he needed. Thanks!
0
lst = [1,2,3,4,5]
# List comp
[x if x % 2 else 0 for x in lst] # when x % 2 is 0 if returns false

# for-loop
for i, x in enumerate(lst):
    if x % 2 == 0:
        lst[i] = 0

#Output
[1, 0, 3, 0, 5]

Comments

0

Using List Comprehension and ternary method :

list1 = [1,2,3,4,5]
list1[:] = [a if a%2 != 0 else 0 for a in list1 ] #  [i if i % 2 == 1 else 0 for i in L]
print list1

Output:

[1, 0, 3, 0, 5]

Notes:

  • By list1[:] I am not creating a new list I am adding to the already existing List
  • What I have done in my LC is I checked if the value is odd then given the same value if not I have given 0

Or for normal method

Code1:

list1 = [1,2,3,4,5]
for i,v in enumerate(list1):    
    if v%2==0:
        list1[i]=0
print list1

Output:

[1, 0, 3, 0, 5]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.