+ 2

indexing a list with nth depths

hi i am trying to make a function that can index a list and its sublists and provide their indices. example - [2, [3,4,5,[6,[7],8] ,[9], 10. . .] so that if it hits a nested list it can output its indices somehow as well. some sample outputs- number | index 2 , (0) 3 , (1) 4 , (1,1) 5, (1,2) or index | number ive tried recursion and its successful but i dont know how to grab their respective indices as a return. just wondering how other humans deal with a problem like this. UPDATE --------------------- in my original problem i was asking about enumerating indices but i think i didnt give enough information. in my hypothetical the indices could be used to 'backtrack' through the master list. so if i wanted 88 from this list a = [2,[3,4,5],[8,[5,6,[88],7],9,10],11,[12,13,[14,15,],28]] i would be able to get a[2][1][2] as 88. maybe is this the wrong way to do it? ( considering if the list goes too many depths down) as always thanks for your time -R https://code.sololearn.com/c3SA3saJpN2n/?ref=app

11th Aug 2021, 1:12 PM
ren[paused]
ren[paused] - avatar
15 Réponses
+ 5
you can use enumerate(). It returns a list of tuples that contain the index and value of each value in an iterable. You can use a loop and isinstance() to check if it's a list, if it is, print out an enumerated version. Please also include specific input and exact output Edit: ren[paused] it's still not entirely clear what you'd like. Can you provide specific input with the exact output?
11th Aug 2021, 1:31 PM
Slick
Slick - avatar
+ 4
It is a case of recursion: https://code.sololearn.com/c763oRZ19EAX
11th Aug 2021, 1:56 PM
Oma Falk
Oma Falk - avatar
+ 2
ren[paused] The index() idea won't work for duplicate values present. Try this instead: a = [2,[3,4,5],[8,[5,6,[88],7],9,10],11,[12,13,[14,15,],28]] def shw_lst(li, max_depth = 99, m = [-1]): for i in range(len(li)): m[-1] += 1 print(f"li[{']['.join(map(str, m))}]".ljust(14), "->", k := li[i], "\n") if max_depth < 2: return if isinstance(k, list): shw_lst(k, max_depth - 1, m + [-1]) shw_lst(a) # I'm too lazy to modify this code to make it into that artwork. # Hope this helps
11th Aug 2021, 2:49 PM
Calvin Thomas
Calvin Thomas - avatar
+ 2
Oma Falk Your code is showing the index incorrectly, or I am missing something... 🤔
11th Aug 2021, 2:50 PM
Solo
Solo - avatar
+ 2
Vasiliy yep... it is depth not index
11th Aug 2021, 2:52 PM
Oma Falk
Oma Falk - avatar
+ 2
ren[paused] Here's my solution: https://code.sololearn.com/cea8meoIeUQM/?ref=app I dunno whether it meets the criteria, but... yeah.
11th Aug 2021, 4:39 PM
Calvin Thomas
Calvin Thomas - avatar
+ 1
hey Slick yea i was looking into that as well, i just wanted a number , index format maybe the indexes can be separated into a tuple for easier reading. my concern was how to grab the indices even if the lists vary in depth when going through the main list
11th Aug 2021, 1:34 PM
ren[paused]
ren[paused] - avatar
+ 1
ren[paused] nice challenge 😁
11th Aug 2021, 2:48 PM
Oma Falk
Oma Falk - avatar
+ 1
✩✮★✮✩ nice! i saw something similar online, but i didnt understand what that use of [-1] was for, wouldnt that mean you are going from the tail end of the list?
11th Aug 2021, 3:14 PM
ren[paused]
ren[paused] - avatar
+ 1
hehe i looked at it again- i see how that would work as an exit clause thats succinct and works
11th Aug 2021, 3:29 PM
ren[paused]
ren[paused] - avatar
+ 1
Not exactly what you want but similar https://code.sololearn.com/czj5sC4wDs0d/?ref=app
19th Aug 2021, 2:32 AM
abhinav
abhinav - avatar
0
in my original problem i was asking about enumerating indices but i think i didnt give enough information. in my hypothetical the indices could be used to 'backtrack' through the master list. so if i wanted 88 from this list a = [2,[3,4,5],[8,[5,6,[88],7],9,10],11,[12,13,[14,15,],28]] i would be able to get a[2][1][2] as 88. maybe is this the wrong way to do it? ( considering if the list goes too many depths down)
11th Aug 2021, 2:44 PM
ren[paused]
ren[paused] - avatar
0
@ Oma Falk im pretty sure you were the inspiration for this one from one of your previous challenges :)
11th Aug 2021, 2:55 PM
ren[paused]
ren[paused] - avatar
0
i have to say thanks for everyones input so far!!!
11th Aug 2021, 3:05 PM
ren[paused]
ren[paused] - avatar
0
it does - you also kept the formatting intent of the original 'gist' which was a nice touch
11th Aug 2021, 4:41 PM
ren[paused]
ren[paused] - avatar