0

I am having trouble with this part with my class assignment. It ask to replace an even element with a 0. I have tried but kept getting error for trying to divide it in anyway. Is there a way to replace even numbers with 0, I might end up adding a user input later on, so have to code where it finds an even number and replace it.

def main():  

    list = [7, 2, 2, 4, 5, 6, 9]

    print(sectionC(list))

def sectionC(list):  
    for n, i in list:
        if list == i//2:
            list[n]=0
main()
Victor Cano
  • 29
  • 1
  • 8

4 Answers4

5

The Errors You're Facing

  • Your function returns nothing. If you want the result of the function to be visible and accessible once finished, you have to return it, otherwise your function will return None by default.

    While it is true that mutable objects like lists are passed effectively by reference, so that modifying them inside your function modifies them everywhere, it is always a good idea to explicitly return a value.

  • You should never name a variable list. list() is an inbuilt function in Python, and by declaring a variable with the same name, you lose the ability to call it. Change your variable to something more appropriate - collection works, for instance.

  • n, i in list doesn't work - lists only let you iterate through one element. So for i in list will work, where i is assigned the value of an element on each iteration.

    If you want to have access to both the index and the value of an element in iteration, you need to use enumerate() like so: for index, item in enumerate(collection).

    enumerate() takes a list and returns a list of tuples, where every tuple is of the form (index of element, corresponding element) in the original list. It is used primarily to facilitate easy iteration over both index and items using the syntax I just referenced.

The Right Way To Solve This

Use a list comprehension:

collection = [7, 2, 2, 4, 5, 6, 9] # renaming list variable 

print([x if x % 2 != 0 else 0 for x in collection])

The Acceptable Way

This is what you were attempting to do, corrected:

def sectionC(collection):  
    for index, item in enumerate(collection): # using better names than n, i
        if item % 2 == 0: # check if element is even
            collection[index] = 0
    return collection

Note that the logic here is identical to the list comprehension. This is because list comprehensions actually expand out to something exactly like this - the benefit is one of conciseness and readability, rather than efficiency.

Community
  • 1
  • 1
Akshat Mahajan
  • 9,543
  • 4
  • 35
  • 44
  • ​​​​​​​​​​​​​​​Note that `List` also isn't a good variable name. – Remi Guan Apr 22 '16 at 03:16
  • @KevinGuan Why is `List` not a good variable name? Does it override something I'm not aware of? – Akshat Mahajan Apr 22 '16 at 03:17
  • ​​​​​​​​​​​​​​​Well, check [this answer](http://stackoverflow.com/a/159745/5299236). And sorry, we should also avoid use`l` as a single variable name as [said in PEP8](http://stackoverflow.com/a/36784169/5299236) (so I edited my comment). – Remi Guan Apr 22 '16 at 03:24
1

Another fancy approach:

A number is even when a&1 is equal to 0, so is odd when is a&1 is 1

def sectionC(list):  
    return [a if a&1 else 0 for a in list]

Hope it helps,

Jonathan Prieto-Cubides
  • 2,577
  • 2
  • 18
  • 17
0

You're so close:

def sectionC(list):  
    for n, i in list:
        if list == i//2:
            list[n]=0

Just add an enumerate() so that you have an index, and change your if statement for an actual even/odd check:

def sectionC(list):  
    for i,n in enumerate(list):
        if not n%2:
            list[i]=0

What enumerate(list) is doing:

Creates an iterable object that prints out tuples that look like (0,7) (1,2) ... in the case of your list

Thats pretty much all your code was missing, but you had the i and n values switched around.
x%2 is the standard check for evenness (it returns 1 for an odd number, 0 for even)

MikeTGW
  • 395
  • 2
  • 10
  • What does enumerate() do? Overall thanks but kind of worried that it may not be acceptable to use since we have not learn it yet. – Victor Cano Apr 22 '16 at 03:12
  • I found when I was learning python that looking at all the functions available (like enumerate) was interesting. Try `dir(__builtins__)` – MikeTGW Apr 22 '16 at 03:16
0

may be this is what you are looking for:

list = [0 if i % 2 == 0 else i for i in list]
Amitkumar Karnik
  • 912
  • 13
  • 23