Replacing an even element with another element

c program to replace all even elements by 0 and odd by 1 in one dimensional array.
replace each element except the first and last by the larger of its two neighbors.
replace odd sequence with count in c
replace even sequence with first in c
c program to replace an element in an array
python list replace element at index
replace odd by even digits
replace element in list python

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()

Replace Python List Elements with Odd Position with another value , By your example you want to replace the even elements, as in python the first element has the index 0. Standard python: l = ['3a', '4a', '5a', '6a',  Appliance Repairs Made Easy. Find The Right Appliance Part With Simple Steps & Save. No Hassle Returns, 365 Day Warranty & Satisfaction Guaranteed.

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)

Replace the odd positioned elements with their cubes and even , Replace the odd positioned elements with their cubes and even positioned Given an array arr[] of n elements, the task is to replace all the odd positioned See your article appearing on the GeeksforGeeks main page and help other Geeks. Get The Products You Need Delivered Straight To Your Door When You Shop Lowe's®. You Can Count on Lowe’s For All Of Your Home Improvement Needs.

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,

How to replace every other element in an array with another array , How to replace every other element in an array Learn more about array replacement, even odd indexes. 1,000,000 Parts and 30,000 Photos. No-Hassle Returns, 1-Year Warranty.

may be this is what you are looking for:

list = [0 if i % 2 == 0 else i for i in list]

C program to replace all EVEN elements by 0 and Odd by 1 in One , C program to replace all EVEN elements by 0 and Odd by 1 in One Dimensional Array - C programming Example. This example will read array and replace all  Check Out Replace Oven Element On eBay. Fill Your Cart With Color Today!

How to conditionally replace items in a list, Then I tried using Python's enumerate (see my previous example) built-in function to replace the item in-line. This seems to be a more elegant  Given an array of integers, replace every element with the next greatest element (greatest element on the right side) in the array. Since there is no element next to the last element, replace it with -1. For example, if the array is {16, 17, 4, 3, 5, 2}, then it should be modified to {17, 5, 5, 5, 2, -1}.

Python list: how can I replace certain items of the list?, If you want to replace the existing value with another o. If it doesn't work then in the loop save the indices of those elements in a different list, and after the first loop For this example, let's consider the case where we need to replace all even  Replacing a faulty element isn't hard, but it can be a little tricky to manipulate some of the smaller parts within the tight confines of the oven. First, shut off the power to the oven at your home's main breaker to ensure that you're working safely. Then, identify and detach the old element.

ArrayList add/replace element at specified index in Java , This method inserts the specified element E at the specified position in this list. It shifts the element currently at that position (if any) and any subsequent elements​  Remove and Replace the Oven Element Remove and Replace the Oven Element Before you begijn, identify the oven manufacturer, the oven model number or the part number on the element. The packaging on the new element will list the manufacturers, model numbers and part numbers for which it serves as a replacement.

Comments
  • ​​​​​​​​​​​​​​​[i if i % 2 else 0 for i in l]?
  • ​​​​​​​​​​​​​​​Note that List also isn't a good variable name.
  • @KevinGuan Why is List not a good variable name? Does it override something I'm not aware of?
  • ​​​​​​​​​​​​​​​Well, check this answer. And sorry, we should also avoid usel as a single variable name as said in PEP8 (so I edited my comment).
  • 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.
  • I found when I was learning python that looking at all the functions available (like enumerate) was interesting. Try dir(__builtins__)