Skip to content
Related Articles

Related Articles

Improve Article

Python – Find starting index of all Nested Lists

  • Last Updated : 31 May, 2021

In this article given a matrix, the task is to write a Python program to compute the starting index of all the nested lists.

Example:

Input : test_list = [[5], [9, 3, 1, 4], [3, 2], [4, 7, 8, 3, 1, 2], [3, 4, 5]]

Output : [0, 1, 5, 7, 13]

Explanation : 1 + 4 = lengths of 2 initial lists = 5, 3, of 3rd list start from 5th index [ 0 based indexing ], 



hence 5. as 3rd element in result list.

Input : test_list = [[5], [9, 3, 1, 4], [3, 2], [3, 4, 5]]

Output : [0, 1, 5, 7]

Explanation : 1 + 4 = lengths of 2 initial lists = 5, 3, of 3rd list start from 5th index [ 0 based indexing ],

hence 5. as 3rd element in result list.

Method #1 : Using loop + len()

In this, length of each sublist is computed using len() and summed, cumulatively, and added as intermediate result. The initial index is derivative of lengths of sublists.

Python3






# Python3 code to demonstrate working of
# Initial element index in Matrix
# Using loop
 
# initializing list
test_list = [[5], [9, 3, 1, 4], [3, 2], [4, 7, 8, 3, 1, 2], [3, 4, 5]]
 
# printing original list
print("The original list is : " + str(test_list))
 
res = []
lens = 0
for sub in test_list:
 
    # lengths of sublist computed
    res.append(lens)
    lens += len(sub)
 
# printing result
print("Initial element indices : " + str(res))

Output:

The original list is : [[5], [9, 3, 1, 4], [3, 2], [4, 7, 8, 3, 1, 2], [3, 4, 5]]

Initial element indices : [0, 1, 5, 7, 13]

Method #2 : Using accumulate() + map() + len()

In this, we perform task of getting summation using accumulate(), map() is used to get lengths of all the sublists computed using len(). 

Python3




# Python3 code to demonstrate working of
# Initial element index in Matrix
# Using accumulate() + map() + len()
from itertools import accumulate
 
# initializing list
test_list = [[5], [9, 3, 1, 4], [3, 2], [4, 7, 8, 3, 1, 2], [3, 4, 5]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# ignoring last index using "-1"
# sum starting at 0
res = [0, *accumulate(map(len, test_list[:-1]))]
 
# printing result
print("Initial element indices : " + str(res))

Output:

The original list is : [[5], [9, 3, 1, 4], [3, 2], [4, 7, 8, 3, 1, 2], [3, 4, 5]]

Initial element indices : [0, 1, 5, 7, 13]

Method #3 : Using type() and loop and a if statement

In this, we simply check the type of the element in the list if it’s another list we print its index otherwise not. This method will work regardless of the number of non-list type elements in the list

Python3




# This will print all the starting indexes
# of sublists inside this list
lis = [[1,2,3],4,5,[6,7,8],9,0,[10]]
 
for i in lis:
    if type(i) == list:
        print(lis.index(i), end=",")
         
# This code is contributed by BABAji

Output:

0,3,6,

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :