Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Adam Harms
9,826 Pointstraversing nested lists, need help in python
Can someone please help me out with how to walk through a list, go into the next level of lists, walk through that one and continue no matter how many levels of lists are in lists? I cant find any references for this, only references if you know what you are looking for ie. you can see the list and you call exactly what you want by location... How do you do it if you are calling a list in a function and you have to use variables to count through the entire list or need to find a name in a 3rd level of a list. This is baffling to me right now and would love a reference to study this. thanks
1 Answer

Alexander Davison
65,469 PointsI don't fully understand your question, but I think maybe this example will help:
lists_of_lists = [[1,2,3], [4,5,6], [7,8,9]]
for x in list_of_lists:
for y in x:
print(y)
This prints:
1
2
3
4
5
6
7
8
9
Adam Harms
9,826 PointsAdam Harms
9,826 PointsThat helps thanks, but what if they are strings and not ints? When I tried that it printed each letter of the string on a separate line. So if I had a list, within a list, that's within a list (3 deep list) and I wanted to print off all lists within the main list how is that done? I feel like I am asking a lot but I am not fully grasping how to run through lists to grab whatever type data I want where ever it is.. or even grab all of it in a readable format.
thanks