1

For instance, how would I define a function so that every odd integer in a list is replaced with the string 'odd'? Thanks

list = [10 , 11, 12, 13, 14, 15, 16,]

Intended result

[10, 'odd', 12, 'odd', 14, 'odd', 16]
2
  • 2
    Have you read about loops and conditionals?. Commented Apr 5, 2016 at 0:15
  • 2
    Note: Please do not name a variable the same as a Python built-in. In this case list then masks the built-in function list(iterator) Commented Apr 5, 2016 at 0:28

5 Answers 5

2

A one line solution can be:

print [(i,'odd')[i%2] for i in list]
Sign up to request clarification or add additional context in comments.

2 Comments

Reverse the tuple for more clarity: [(x,'odd')[x%2] for x in list]
It is also better to use a tuple (x,'odd') rather than a list `[x,'odd'] since the tuple is constructed once and the list each and every time through the loop...
1

I feel like you should probably get the solution on your own, but anyway...

result = []
for i in list:
    if i % 2 != 0:
        result.append('odd')
    else:
        result.append(i)

Or, maybe something to push you forward:

def turn_odd(i):
    if i % 2 != 0:
        return 'odd'
    else:
        return i

list = [10 , 11, 12, 13, 14, 15, 16,]
result = map(lambda x: turn_odd(x), list)

Comments

0
for i in range(len (list)):
    if (list[i] % 2 != 0):
        list[i] = "odd"

4 Comments

A first argument of 0 to range may be omitted. I.e., one would normally just write range(len(list)).
Okay. I haven't used python in a while. I'll change it now
It is better to do for i, x in enumerate(list): if x%2: list[i]='odd'
@Reti43 It is not the parity of the index to check -- it is the parity of the object inside. So your code is wrong...
0

You can use list comprehension:

list = [10 , 11, 12, 13, 14, 15, 16]
new_list = [i if i % 2 == 0 else "odd" for i in list]

Here's a good example of how it works: if/else in Python's list comprehension?

2 Comments

Technically this isn't what OP asked for. OP asked to replace the odd elements of a list, not generate a new list with the odd elements replaced. Whether this matters to OP or not is unclear, but it does create a new list.
You could do list[:] = ... instead of new_list = ... You shouldn't be using list as a variable name anyway because it shadows the built-in type. You could also do ["odd" if i % 2 else i for i in list]
0

First note that list is reserved for the type list in Python, so I will use my_list instead. Then you have three options:

A loop with a conditional

for i in range(0, len(my_list)):
    if (i % 2 != 0):
        my_list[i] = "odd"

List comprehension (same logic as above)

my_list = ["odd" if i % 2 != 0 else my_list[i] for i in range(len(my_list))]

A loop without a conditional

for i in range(1, len(my_list), 2):
     my_list[i] = "odd"

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.