How do you return only odd numbers in python?

My issue here is that the code filters out the even numbers correctly which is what I want, however it stops at seven and doesn't display number 9 which is what I would expect it to do. I've tried going over my code but I can't seem to find the issue

def remove_even[numbers] :
    new_list = []
    for i in range[0,len[numbers]-1] :
        if i % 2 != 0 :
            new_list.append[i]
    return new_list
l = [1,2,3,4,5,6,7,8,9,10]
print[remove_even[l]]

asked Jan 16, 2017 at 12:14

7

You should just directly loop through your values instead of indices

for i in numbers:

Otherwise if you wanted to use range you would have to index into your list

for i in range[0, len[numbers]]:
    if numbers[i] % 2 != 0 :
        new_list.append[numbers[i]]

For brevity, list comprehensions are well-suited for this type of task

>>> new_list = [num for num in l if num % 2 == 1]
>>> new_list
[1, 3, 5, 7, 9]

answered Jan 16, 2017 at 12:15

Cory KramerCory Kramer

109k15 gold badges156 silver badges206 bronze badges

0

[k for k in l if k %2]

Is a simple list comprehension that returns

[1, 3, 5, 7, 9]

PM 2Ring

52.9k5 gold badges77 silver badges167 bronze badges

answered Jan 16, 2017 at 12:16

e4c5e4c5

51.3k11 gold badges95 silver badges131 bronze badges

0

You can do this more easily with the filter built-in function:

list[filter[lambda x: x % 2, l]]
[1, 3, 5, 7, 9]

It will only retain elements for which the condition evaluates to True

answered Oct 14, 2020 at 18:00

Nicolas GervaisNicolas Gervais

30.5k11 gold badges102 silver badges127 bronze badges

This is because you are starting your range[] function of your for loop from 0 and ending at len[numbers]-1 [which is 9 in your case], python range[] already will run till end-1:

for eg:

for i in range[0,9]:
    print[i]

will print no's: 0 1 2 3 4 5 6 7 8

and that's why your 9 is not here in the output.

you don't have to start your loop from 0. If you are starting from 0 you can arrange your for loop like this:

1]

for i in range[0, len[numbers]+1]

2]Or you can code like more pythonic way.

def remove_even[numbers] :
    new_list = []
    for i in numbers :
        if i % 2 != 0 :
            new_list.append[i]
    return new_list

answered Jan 16, 2017 at 13:06

Ajay RawatAjay Rawat

1422 silver badges12 bronze badges

1

#simpliest way of doing it
mylist = [1,2,3,4,5,6,7,8,9,10,11]
for x in mylist:
    if x % 2 == 1: #this displays odd numbers
        print[x]

answered Jul 3, 2018 at 14:55

numbers = [1,2,3,4,5,6,7,8,9,10]

odds = [i for i in numbers if i%2!=0]

answered Jan 16, 2017 at 12:16

def odd_numbers[n]:return [x for x in range[1,n+1] if x%2==1]print[odd_numbers[5]] # Should print [1, 3, 5]

answered Jul 18, 2020 at 10:23

1

def remove_even[numbers] :
    new_list = []
    for i in range[0,len[numbers]-1] :
        if i % 2 != 0 :
            new_list.append[i]
    return new_list 

l = [1,2,3,4,5,6,7,8,9,10] 
print[remove_even[l]]

you got the algorithm right in the money. All you need to change is -1 to +1 instead and you will get your desired result.

why change the logic algorithm if only one item is to be changed.

answered Mar 14, 2021 at 22:17

1

It's simple using list comprehension:

def odd_no[n]:
      return [x for x in range[n+1] if n%2==1]

answered Oct 14, 2020 at 17:30

1

How do you return only odd numbers in a list Python?

The simplest way to print odd numbers from a list of consecutive numbers is with the extended syntax of the slice operator. The slice operator returns a fragment of a list given a start index [included] and an end index [not included].

How do you show odd numbers in Python?

See this example:.
num = int[input["Enter a number: "]].
if [num % 2] == 0:.
print["{0} is Even number". format[num]].
print["{0} is Odd number". format[num]].

How do you extract even and odd numbers from a list in Python?

Step 1 : create a user input list. Step 2 : take two empty list one for odd and another for even. Step 3 : then traverse each element in the main list. Step 4 : every element is divided by 2, if remainder is 0 then it's even number and add to the even list, otherwise its odd number and add to the odd list.

How do you extract an odd number from an array in Python?

This Python Program uses the for loop range to Print the Odd Numbers in a Numpy Array. The if statement [if [oddArr[i] % 2 != 0]] checks the numpy array item at each index position is not divisible by two. If True, [print[oddArr[i], end = ” “]] print that numpy Odd array number.

Chủ Đề