Python: Select the odd items of a list
Python List: Exercise - 46 with Solution
Select Odd Items from List
Write a Python program to select the odd items from a list.
Visual Presentation of Odd Numbers:
Sample Solution:
Python Code:
# Define a list 'x' containing numeric elements from 1 to 9
x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# Use slicing to create a new list containing every second element of 'x'
# The slice notation 'x[::2]' starts at the beginning, ends at the end, and steps by 2
# It selects every second element in the list
# Print the resulting list
print(x[::2])
Sample Output:
[1, 3, 5, 7, 9]
Flowchart:
Odd Numbers between 1 to 100:
Python Code Editor:
Previous: Write a Python program to convert a pair of values into a sorted unique array.
Next: Write a Python program to insert an element before each element of a list.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/list/python-data-type-list-exercise-46.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics