calculate the total number of odd elements in a list

Question:

arr=[13,15,4,6,8,71,45,54,56,77,67,88,49,33838,3784376,77735] 
for num in arr:
  if num % 2 != 0:
    a = sum(num)
    print(a)

Output:

TypeError: ‘int’ object is not iterable

Asked By: khôi lê

||

Answers:

try using modulo and list comprehension

arr=[13,15,4,6,8,71,45,54,56,77,67,88,49,33838,3784376,77735]

def odd_elements(arr):
    return len([x for x in arr if x % 2 != 0])

print(odd_elements(arr))

or, using sum()

arr=[13,15,4,6,8,71,45,54,56,77,67,88,49,33838,3784376,77735]


def odd_elements(arr):
    return sum(1 for i in arr if i % 2)

print(odd_elements(arr))
Answered By: py9

You cannot call sum on an int (num). You can do the following:

arr = [13, 15, 4, 6, 8, 71, 45, 54, 56, 77, 67, 88, 49, 33838, 3784376, 77735] 
count = 0
for num in arr:
  if num % 2 != 0:
    count += 1
    print(count)

or more simply:

print(sum(num % 2 == 1 for num in arr))
Answered By: jprebys

You can do with list comprehension,

In [1]: sum([i for i in arr if i %2 != 0])
Out[1]: 78072

And, sum(int) is not possible because sum only takes iterable (list, tuple, etc.) as the argument.

Answered By: Rahul K P

sum only accepts iterables, not single items.

try this:

a = a + num

take a look at this link to learn more about sum.

Answered By: Mehrdad Pedramfar

You need to remove a = sum(num). sum() takes an array but you have given it a int.

The code should be like this:

total = 0
arr=[13,15,4,6,8,71,45,54,56,77,67,88,49,33838,3784376,77735] 
for num in arr:
  if num % 2 != 0:
    total = total + 1
print(total)
Answered By: Rafat Hasan

You are looping over the list, thus num is the current element in the iteration. You want to use sum on an iterable such as below:

> arr = [13,15,4,6,8,71,45,54,56,77,67,88,49,33838,3784376,77735]
> sum(num % 2 == 0 for num in arr)
8
Answered By: Jab

I think you can get them with a list comprehension:

arr=[13,15,4,6,8,71,45,54,56,77,67,88,49,33838,3784376,77735]
elems = sum([ 1 if num %2 != 0 else 0 for num in arr])
print(f"Number of odd elements: {elems}")

Checkout as you are trying to sum the value of the "num" itself. Not sure if you were trying to append it to a list onto a.

Answered By: Laura Uzcategui

im no sure what you mean by total number of odd elements as in all the times theres a odd number or the sum of all the odd numbers but here

arr=[13,15,4,6,8,71,45,54,56,77,67,88,49,33838,3784376,77735] 
a = 0
for num in arr:
  if num % 2 != 0:
    a += num
print(a)

sum is used on lists so if you realy wanted to use the sum it looks like this

arr=[13,15,4,6,8,71,45,54,56,77,67,88,49,33838,3784376,77735] 
a = []
for num in arr:
  if num % 2 != 0:
    a.append(num)
print(sum(a))
Answered By: christopher

The problem is due because "num" is just an element inside your list and sum functions needs to receive an iterable, if you just want to count the odd elements inside arr you can use:

If you want to count the odd elements

oddsCounter = len([x for x in arr if x % 2 != 0])
print(oddsCounter)

If you want to get the total sum

oddsSum = sum([x for x in arr if x % 2 != 0])
print(oddsSum)
Answered By: Pedro Fumero

The sum function takes a list as an argument and sums all the numbers inside that list.
Instead of passing a list, you passed an integer (at line 4):

a=sum(num)

num is an integer, not a list.
What you might want to do to count the number of odd numbers in a list is this:

odd_numbers_count = 0 #keeps track of how many odd numbers there are
arr=[13,15,4,6,8,71,45,54,56,77,67,88,49,33838,3784376,77735] 
for num in arr:
  if num % 2 != 0:
    odd_numbers_count += 1 # every time you encounter an odd number, it adds one to the count
print(f'Total number of odd elements: {odd_numbers_count}')
Answered By: Glacialyx
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.