Python | Integer count in Mixed List
Last Updated :
11 May, 2023
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
test_list = [ 3 , 'computer' , 5 , 'geeks' , 6 , 7 ]
print ( "The original list is : " + str (test_list))
res = len ( list (i for i in test_list if isinstance (i, int )))
print ( "The length of integers in list is : " + str (res))
|
Output
The original list is : [3, 'computer', 5, 'geeks', 6, 7]
The length of integers in list is : 4
The time complexity of the above program is O(n), where n is the length of the input list.
The space complexity of the program is O(1), which is constant. This is because only a few variables are used to store the input list.
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
test_list = [ 3 , 'computer' , 5 , 'geeks' , 6 , 7 ]
print ( "The original list is : " + str (test_list))
temp = list ( map ( lambda i: isinstance (i, int ), test_list))
res = len ([ele for ele in temp if ele])
print ( "The length of integers in list is : " + str (res))
|
Output
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 length of the list test_list
Auxiliary Space: O(1) additional space is not needed
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
test_list = [ 3 , 'computer' , 5 , 'geeks' , 6 , 7 ]
print ( "The original list is : " + str (test_list))
res = []
for i in test_list:
if ( type (i) is int ):
res.append(i)
x = len (res)
print ( "The length of integers in list is : " + str (x))
|
Output
The original list is : [3, 'computer', 5, 'geeks', 6, 7]
The length of integers in list is : 4
Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
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
def is_int(val):
return isinstance (val, int )
test_list = [ 3 , 'computer' , 5 , 'geeks' , 6 , 7 ]
print ( "The original list is : " + str (test_list))
res = len ( list ( filter (is_int, test_list)))
print ( "The length of integers in list is : " + str (res))
|
Output
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
Method #4: Using try and except method.
Algorithm:
- Initialize a counter variable res to zero.
- Loop through each item i in the list test_list.
- For each item i, check if its type is int.
- If i is of type int, increment the counter variable res.
- Finally, print out the value of the counter variable res as the length of integers in the list.
Python3
test_list = [ 3 , 'computer' , 5 , 'geeks' , 6 , 7 ]
try :
res = 0
for i in test_list:
if type (i) = = int :
res + = 1
print ( "The length of integers in list is : " + str (res))
except Exception as e:
print ( "An error occurred: " + str (e))
|
Output
The length of integers in list is : 4
Time complexity:
The time complexity of this algorithm is O(n), where n is the length of the input list test_list. This is because the algorithm needs to loop through each item in the list once in order to count the number of integers in the list.
Auxiliary Space:
The auxiliary space complexity of this algorithm is O(1) because it only uses a constant amount of additional space to store the counter variable res and the loop variable i. No additional data structures are used to store the input list or the intermediate results, so the space complexity is constant with respect to the input size.
Method #5: Using isdigit()
Approach:
- Initialize a variable count to 0.
- Loop through the elements in the list using a for loop.
- Check if the element is a digit or not using the isdigit() method.
- If the element is a digit, increment the count variable by 1.
- Return the count variable after the loop ends.
Python3
lst = [ 3 , 'computer' , 5 , 'geeks' , 6 , 7 ]
print ( "The original list is: " ,lst)
count = 0
for elem in lst:
if str (elem).isdigit():
count + = 1
print ( "The length of integers in list is :" , count)
|
Output
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 length of the input list.
Space complexity: O(1)
Similar Reads
Python - Convert Number to List of Integers
We need to split a number into its individual digits and represent them as a list of integers. For instance, the number 12345 can be converted to the list [1, 2, 3, 4, 5]. Let's discuss several methods to achieve this conversion. Using map() and str() Combination of str() and map() is a straightforw
3 min read
Python List Counting Programs
Python provides various methods, such as count(), dictionary-based counting, and list comprehensions, to efficiently handle counting operations. This collection of Python programs covers different ways to count elements in lists, including counting occurrences, matching elements, frequency analysis,
2 min read
Python - Binary list to integer
A binary list represents binary digits (0s and 1s) as individual elements of a list. This article will explore various methods to convert a binary list into an integer. Using int() with String ConversionThis is the most efficient method. By joining the binary list into a string and using the built-i
3 min read
Python | Convert Integral list to tuple list
Sometimes, while working with data, we can have a problem in which we need to perform type of interconversions of data. There can be a problem in which we may need to convert integral list elements to single element tuples. Let's discuss certain ways in which this task can be performed. Method #1 :
3 min read
Count unique items in a list
There are several methods for finding or counting unique items inside a list in Python. Here we'll discuss 3 methods. Brute Force ApproachThe first method is the brute force approach.We traverse from the start and check the items. If the item is not in the empty list(as it has taken empty) then we w
3 min read
Python - Count elements in tuple list
Sometimes, while working with data in form of records, we can have a problem in which we need to find the count of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method #1: Using len()
5 min read
Python - Count similar pair in Dual List
Sometimes, while working with data, we can have a problem in which we receive the dual elements pair and we intend to find pairs of similar elements and it's frequency. This kind of data is usually useful in data domains. Let's discuss certain ways in which this task can be performed.Method #1 : Usi
4 min read
Iterate Over a List of Lists in Python
We are given a list that contains multiple sublists, and our task is to iterate over each of these sublists and access their elements. For example, if we have a list like this: [[1, 2], [3, 4], [5, 6]], then we need to loop through each sublist and access elements like 1, 2, 3, and so on. Using Nest
2 min read
Python - Elements frequency count in multiple lists
Sometimes while working with Python lists we can have a problem in which we need to extract the frequency of elements in list. But this can be added work if we have more than 1 list we work on. Let's discuss certain ways in which this task can be performed. Method #1: Using dictionary comprehension
6 min read
Python | List Element Count with Order
Sometimes, while working with lists or numbers we can have a problem in which we need to attach with each element of list, a number, which is the position of that element's occurrence in that list. This type of problem can come across many domains. Let's discuss a way in which this problem can be so
4 min read