Find Numbers with Even Number of Digits in Python


Suppose we have a list of numbers. We have to count the numbers that has even number of digit count. So if the array is like [12,345,2,6,7896], the output will be 2, as 12 and 7896 has even number of digits

To solve this, we will follow these steps −

  • Take the list and convert each integer into string
  • if the length of string is even, then increase count and finally return the count value

Example

Let us see the following implementation to get better understanding −

 Live Demo

class Solution(object):
   def findNumbers(self, nums):
      str_num = map(str, nums)
      count = 0
      for s in str_num:
         if len(s) % 2 == 0:
            count += 1
      return count
ob1 = Solution()
print(ob1.findNumbers([12,345,2,6,7897]))

Input

[12,345,2,6,7897]

Output

2

Updated on: 29-Apr-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements