Open In App

Python | Indexing a sublist

Improve
Improve
Like Article
Like
Save
Share
Report

In Python, we have several ways to perform the indexing in list, but sometimes, we have more than just an element to index, the real problem starts when we have a sublist and its element has to be indexed. Let’s discuss certain ways in which this can be performed. 

Method #1 : Using index() + list comprehension This method solves this problem in 2 parts, for the first part it generates a new list and then it performs indexing on it. 

Python3




# Python3 code to demonstrate
# indexing of sublist
# using list comprehension + index()
 
# initializing test list
test_list = [[1, 'Geeks'], [2, 'For'], [3, 'Geeks']]
 
# printing original list
print("The original list : " + str(test_list))
 
# using list comprehension + index()
# indexing of sublist
res = [ele for i, ele in test_list].index('For')
 
# print result
print("Index of nested element is : " + str(res))


Output : 

The original list : [[1, 'Geeks'], [2, 'For'], [3, 'Geeks']]
Index of nested element is : 1

Time complexity : O(n), where n is length of test_list

Auxiliary space : O(n), where n is number of sub list in test_list.

  Method #2 : Using next() + enumerate() This problem can be solved in an efficient manner using the combination of the above functions. The next function checks for the elements and enumerate functions separates the nested list elements. 

Python3




# Python3 code to demonstrate
# indexing of sublist
# using enumerate() + next()
 
# initializing test list
test_list = [[1, 'Geeks'], [2, 'For'], [3, 'Geeks']]
 
# printing original list
print("The original list : " + str(test_list))
 
# using enumerate() + next()
# indexing of sublist
res = next((i for i, (j, ele) in enumerate(test_list) if ele == 'For'), None)
 
# print result
print("Index of nested element is : " + str(res))


Output : 

The original list : [[1, 'Geeks'], [2, 'For'], [3, 'Geeks']]
Index of nested element is : 1

Method 3: Use a simple for loop

Use a simple for loop to iterate through each sublist in the main list and check if the desired element is present in it.

Python3




# Python3 code to demonstrate
# indexing of sublist
# using a simple for loop
 
# initializing test list
test_list = [[1, 'Geeks'], [2, 'For'], [3, 'Geeks']]
 
# printing original list
print("The original list : " + str(test_list))
 
# using a simple for loop
# indexing of sublist
res = None
for i in range(len(test_list)):
    if 'For' in test_list[i]:
        res = i
        break
 
# print result
print("Index of nested element is : " + str(res))


Output

The original list : [[1, 'Geeks'], [2, 'For'], [3, 'Geeks']]
Index of nested element is : 1

Time complexity: O(n*m) where n is the length of the main list and m is the maximum length of any sublist. 
Auxiliary space: O(1) as we are not using any additional data structures to store intermediate results.



Last Updated : 25 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads