List and for loop : matrix (practice problem 13)

Hi sir/ma’am, my doubt here is that the output should be in matrix form but its isn’t. And secondly, in the solution it is written that “In the for loop we used _ instead
of a variable name. This can be done when
we don’t use the iteration variable.” So we can use _ instead of variable is that what it means?

Screen Link:
https://app.dataquest.io/m/1009/lists-and-for-loops-practice-problems/13/matrix

My Code:

for i in range(7):
    matrix_of_ones.append([1,1,1])
    
print(matrix_of_ones)

What I expected to happen:
[[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]]

What actually happened:

[[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]]

It is still the equivalent of a matrix form. It is a list of lists, where each inner list can be considered a row.

The printed format is how Python outputs it and it’s nothing except how it’s displayed. The structure, the list of lists, is still the same.

If you wish to you can print it out in the format you require, but that will only serve the purpose of how it looks in the output, and nothing else.

If for a for loop you don’t have any use of the iteration variable, you can use _ instead. It’s like a “throwaway” variable name which is ignored. For example, in your code above -

You are not using i inside of the for loop. So, you could instead use a _.

Also, for any future questions I would request a couple of things -

  1. Make sure your Post title is descriptive of the problem/question.
  2. Try to have one question per post.

The above two make it easier for other students to find similar queries instead of going through every post corresponding to the topic tags.

@the_doctor thanks. I thought that i was doing the code wrong. Also i do search before posting and if i am not satisfied than only i post doubt. I had in my mind that if i have two doubts for one problem then i would ask in one post rather than creating two posts to reduce the hassle for community, that was my only intention. But as per your guidance i will post separately is that correct? Sorry for the inconvenience.

@the_doctor waiting for your reply on this so that i can further proceed in posting my doubts in post. Also i am new here so i’ll try my best to keep the title precise but i can’t promise that because i am struggling with the problem in the first place. Apart from the title, i am following each rule inside the post block.

Hi. As I said, for future posts try to have separate posts for separate question, or at least try to make sure the post title is descriptive enough (yet concise) of your questions.

By asking multiple unrelated questions in the same post, it makes it difficult for other students to find answers to questions they might have. Hence, the suggestion.

If you have more questions that are unrelated to the ones you already asked here, then you should create new posts for those.