Sometimes, while working with data, we can have a problem in which we need to check for the occurrences of a particular data type. In this, we can also have a problem in which we need to check for string occurrences. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using isinstance() + list comprehension
The combination of above methods can be used to perform this task. In this, we check for each element of list for string instance and built list with only string and return it’s length.
# Python3 code to demonstrate working of # Check String occurrences in mixed list # using isinstance() + list comprehension # initialize list test_list = ['gfg', 1, True, 'is', 2, 'best'] # printing original list print("The original list : " + str(test_list)) # Check String occurrences in mixed list # using isinstance() + list comprehension res = len([val for val in test_list if isinstance(val, str)]) # printing result print("Number of strings in list : " + str(res)) |
The original list : ['gfg', 1, True, 'is', 2, 'best'] Number of strings in list : 3
Method #2 : Using sum() + isinstance() + generator expression
The combination of above functionalities can be used to perform this task. In this, we compute the True instances of string check and return it’s sum counting string instances.
# Python3 code to demonstrate working of # Check String occurrences in mixed list # using sum() + isinstance() + generator expression # initialize list test_list = ['gfg', 1, True, 'is', 2, 'best'] # printing original list print("The original list : " + str(test_list)) # Check String occurrences in mixed list # using sum() + isinstance() + generator expression res = sum(isinstance(ele, str) for ele in test_list) # printing result print("Number of strings in list : " + str(res)) |
The original list : ['gfg', 1, True, 'is', 2, 'best'] Number of strings in list : 3
Recommended Posts:
- Python | Convert mixed data types tuple list to string list
- Python | Integer count in Mixed List
- Python | Find Mixed Combinations of string and list
- Python | Lexicographically smallest string in mixed list
- Python | Multiply Integer in Mixed List of string and numbers
- Python | Convert numeric String to integers in mixed List
- Python - Numeric Sort in Mixed Pair String List
- Python - Convert delimiter seperated Mixed String to valid List
- Python | Count occurrences of an element in a list
- Python | Count occurrences of a character in string
- Sort mixed list in Python
- Python - Type conversion in Nested and Mixed List
- Python - Maximum column values in mixed length 2D List
- Python | Segregate Positive and Negative Integers from mixed string
- Count occurrences of a word in string
- Count occurrences of a sub-string with one variable character
- Python - Count Strings with substring String List
- Python | Remove all occurrences in nested list
- Remove all the occurrences of an element from a list in Python
- Python | Count occurrences of an element in a Tuple
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

