w3resource

Python: Select the odd items of a list


Select Odd Items from List

Write a Python program to select the odd items from a list.

Visual Presentation of Odd Numbers:

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:

Flowchart: Select the odd items of a list

Odd Numbers between 1 to 100:

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.



Follow us on Facebook and Twitter for latest update.