How to use isinstance() function In Python
Python3
# create a list of names and marks list1 = [ 'sravan' , 98 , 'harsha' , 'jyothika' , 'deepika' , 78 , 90 , 'ramya' ] # display print (list1) list2 = [] # iterate through list of elements for i in list1: # check for type is str if ( isinstance (i, str )): # display index list2.append(list1.index(i)) print (list2) |
Output
['sravan', 98, 'harsha', 'jyothika', 'deepika', 78, 90, 'ramya'] [0, 2, 3, 4, 7]
The time and space complexity of all methods are:
Time Complexity: O(n)
Space Complexity: O(n)
Python – Find Index containing String in List
Given a list, the task is to write a Python Program to find the Index containing String.
Example:
Input: [‘sravan’, 98, ‘harsha’, ‘jyothika’, ‘deepika’, 78, 90, ‘ramya’]
Output: 0 2 3 4 7
Explanation: Index 0 2 3 4 7 contains only string.