When using GitHub mode, paste your repository URL below and click Save URL to store it. The saved URL will be automatically included with every message you send until you choose to clear it.
Learn more
Your GitHub repository URL is saved. LSBot will automatically fetch your latest code from this repository for each message. To change the URL, clear it first and save a new one.
Exercise 6
Let's try another variation on the even/odd-numbers theme.
We'll return to the simpler one-dimensional version of my_list. In this problem, you should write code that creates a new list with one element for each number in my_list. If the original number is an even, then the corresponding element in the new list should contain the string 'even'; otherwise, the element should contain 'odd'.
my_list = [
1, 3, 6, 11,
4, 2, 4, 9,
17, 16, 0,
]
result = []
for number in my_list:
if number % 2 == 0:
result.append('even')
else:
result.append('odd')
print(result)
Our approach is straightforward: we iterate over all the numbers in the list and check whether each is even. Based on the result, we append either 'even' or 'odd' to the result list.
You may have struggled if you tried to use a list comprehension for this problem. Since comprehensions don't have an else capability, trying to generate 'even' for some values and 'odd' for others is challenging. You can use a ternary expression in the comprehension, but this is a little confusing visually:
my_list = [
1, 3, 6, 11,
4, 2, 4, 9,
17, 16, 0,
]
#highlight
result = [ 'even' if number % 2 == 0 else 'odd'
for number in my_list ]
#endhighlight
print(result)
On line 7, we've used a ternary expression to choose between the two values. The ternary is equivalent to:
if number % 2 == 0:
return 'even'
else:
return 'odd'
A cleaner approach is to use a helper function to determine whether we should add 'even' or 'odd' to the new list:
my_list = [
1, 3, 6, 11,
4, 2, 4, 9,
17, 16, 0,
]
#highlight
def odd_or_even(number):
return 'even' if number % 2 == 0 else 'odd'
result = [ odd_or_even(number)
#endhighlight
for number in my_list ]
print(result)
Video Walkthrough
Hi! I'm LSBot. I can help you think through the selected exercise by giving you hints and guidance without revealing the solution. Your code from the editor will be automatically detected.
Want to know more? Refer to the
LSBot User Guide
.
Detected solution
Loading...
Submit your solution for LSBot review.
Hi! I'm LSBot. Your code from the editor will be automatically detected. I'll review your solution and provide feedback to help you improve. Ask questions about your solution or request a comprehensive code review. Want to know more? Refer to the
LSBot User Guide
.
Detected solution
Loading...
Output
No output yet. Click "Run Code" to execute your code.
You can write code blocks using three backticks (```) followed by the language you want to show.
For instance, if you wanted to write a Ruby code block you would write:
```ruby
class Foo
end
```
class Foo
end
You can also leave out the language identifier, doing this will make it so your code isn't highlighted at all, and all text will be white with no coloring.
```
class Bar
end
```
class Bar
end
Highlighting
You may highlight lines within your code blocks. This is a different type of highlighting, and isn't related to how the code within your code block may be colored depending on the language identifier used.
To highlight one or more lines of code, add # highlight on a separate line just before the first line you wish to highlight. To end highlighting, add # endhighlight on a separate line immediately after the last line you wish to highlight.