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

Python

Need some clarification.

new_list = []

undersc = '_'

for i in range(1, rows + 1):

      new = []

      for j in range(1, columns + 1)     

        new.append(undersc if j > i else 'Z')

      new_list.append(new)

for c in range(len(new_list[0])): 

     print()

for r in range(len(new_list)):

     for c in range(len(new_list[0]))       

       array = print((new_list[r][c]), end=" ")   

     print()

return array

My confusion comes from the last for loops: for r in range(len(new_list)): and for c in range(len(new_list[0])). what does (len(new_list[0])) in this examples measure? Any help would be greatly appreciated.

[MOD: added ```python markdown formatting -cf]

Where is this code from? What are you rows and columns variables referring to? It seems like you're performing some kind of operation on a matrix, but it would help to have access to the matrix.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

From the code, new_list is a list of lists. The two range arguments mean:

  • len(new_list) is the length, or number of items in new_list
  • len(new_list[0]) is the length or number of items in the first element of new_list

In other words, given the list with two elements, each being a list:

new_list = [ ['a', 'b', 'c'], [1, 2, 3, 4] ]

len(new_list) is 2, while new_list[0] is ['a', 'b', 'c'] and len(new_list[0]) is 3