Python - Find starting index of all Nested Lists - GeeksforGeeks

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 listtest_list = [[5], [9, 3, 1, 4], [3, 2], [4, 7, 8, 3, 1, 2], [3, 4, 5]] # printing original listprint("The original list is : " + str(test_list)) res = []lens = 0for sub in test_list:     # lengths of sublist computed    res.append(lens)    lens += len(sub) # printing resultprint("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 listtest_list = [[5], [9, 3, 1, 4], [3, 2], [4, 7, 8, 3, 1, 2], [3, 4, 5]] # printing original listprint("The original list is : " + str(test_list)) # ignoring last index using "-1"# sum starting at 0res = [0, *accumulate(map(len, test_list[:-1]))] # printing resultprint("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 listlis = [[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,
as904465
Python list-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Split string into list of characters
Python | Convert a list to dictionary
How to print without newline in Python?