Python | Integer count in Mixed List
The lists in python can handle different type of data types in it. The manipulation of such lists is complicated. Sometimes we have a problem in which we need to find the count of integer values in which the list can contain string as a data type i.e heterogeneous. Let’s discuss certain ways in which this can be performed.
Method #1 : Using list comprehension + len() + isinstance()
This particular problem can be solved by filtering our search of len using the isinstance method, we can filter out the integer value and then can use len function to get required length value.
Python3
# Python3 code to demonstrate# Integer count in Mixed List# using list comprehension + len() + isinstance()# initializing listtest_list = [3, 'computer', 5, 'geeks', 6, 7]# printing original listprint ("The original list is : " + str(test_list))# using list comprehension + len() + isinstance()# Integer count in Mixed Listres = len(list(i for i in test_list if isinstance(i, int)))# printing resultprint ("The length of integers in list is : " + str(res)) |
The original list is : [3, 'computer', 5, 'geeks', 6, 7] The length of integers in list is : 4
Method #2 : Using lambda + map() + len() + isinstance()
The above problem can also be solved using the lambda function as a map() in the len() along with the isinstance method which performs the task of checking for integer values.
Python3
# Python3 code to demonstrate# Integer count in Mixed List# using lambda + map() + len() + isinstance()# initializing listtest_list = [3, 'computer', 5, 'geeks', 6, 7]# printing original listprint ("The original list is : " + str(test_list))# using lambda + map() + len() + isinstance()# Integer count in Mixed Listtemp = list(map(lambda i: isinstance(i, int), test_list))res = len([ele for ele in temp if ele])# printing resultprint ("The length of integers in list is : " + str(res)) |
The original list is : [3, 'computer', 5, 'geeks', 6, 7] The length of integers in list is : 4
Method #3 : Using type() and len() methods
The above problem can be solved by using type() to check if the type of the element as int, then append that item in a new list and print the size of the new list.
Python3
# Python3 code to demonstrate# Integer count in Mixed List# initializing listtest_list = [3, 'computer', 5, 'geeks', 6, 7]# printing original listprint("The original list is : " + str(test_list))# Integer count in Mixed Listres = []for i in test_list: if(type(i) is int): res.append(i)x = len(res)# printing resultprint("The length of integers in list is : " + str(x)) |
The original list is : [3, 'computer', 5, 'geeks', 6, 7] The length of integers in list is : 4
Method #4: Using filter() and len() method
This approach is similar to method 1, but instead of using list comprehension, we use filter() function to filter out the integers.
Python3
# Python3 code to demonstrate# Integer count in Mixed Listdef is_int(val): return isinstance(val, int)# initializing listtest_list = [3, 'computer', 5, 'geeks', 6, 7]# printing original listprint("The original list is : " + str(test_list))res = len(list(filter(is_int, test_list)))# printing resultprint("The length of integers in list is : " + str(res)) |
The original list is : [3, 'computer', 5, 'geeks', 6, 7] The length of integers in list is : 4
Time Complexity: O(n) where n is the number of elements in the list
Auxiliary Space: O(n) where n is the number of integers in the list






Please Login to comment...