Badges: 3
Report Thread starter 1 year ago
Hi there. I'm usually not too bad with for loops but in this question I have, I have no idea how what to do for this particular question. It is really frustrating me as I can't do all the following questions without this.

The question is to do with a for loop for a matrix so that the matrix's simension can vary.
Im really really stuck and any advice would make me feel a lot better.

I don't know how to write a matrix in this so I could email you
Badges: 19
Report 1 year ago
One way to represent a 'matrix' in python is by creating a list-of-lists (sometimes also known as a 'two-dimensional' list).

Question: What's the difference between a list on its own, and a list which exists inside a list?
Answer: Nothing! They work in exactly the same way.

So look at how a simple one-dimensional list works, and then apply that to a list inside a list.
# length could be anything, but 5 is a good number.
length = 5 

# First, create the list
oneDimensional = [] 

# Secondly, populate the list, one element at a time from 0 up to 5
for i in range(length):
So that's a simple way to create a single list. A single for-loop is used for repeating over a list.

A list-of-lists is just the same as above, but repeated many times. So if a two-dimensional list is a list repeated many times, then the 'for' loop also needs to be repeated many times. A list-within-a-list involves a loop-within-a-loop.
# The "outer" two-dimensional list can be any width, but 4 is a good number 
width = 4 

 # Create the outer list 
twoDimensional = []

 # Now create each of the individual lists and put them inside the outer list.
# This will create 4 inner-lists 
for j in range(width):

  # As before, 5 for an inner-list seems like a good number. 
  length = 5 

  # As before, create a new list 
  oneDimensional = [] 

  # As before, populate the list with data 
  for i in range(length): 
    oneDimensional.append(i * j)

  # Lastly, add the new inner-list to the outer-list 
Actually there are better ways to do it than this. This is the long way of doing it. The concepts of "nesting" lists and "nesting" for loops are important enough to show the long way however.

Have a read here for some more examples. You'll see some examples using list comprehensions here, which shortens the syntax a lot:

Or here (more examples using the shortened list comprehension format too):

Quick Reply

Attached files
Write a reply...
new posts
to top
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.


University open days

  • Sheffield Hallam University
    Get into Teaching in South Yorkshire Undergraduate
    Wed, 26 Feb '20
  • The University of Law
    Solicitor Series: Assessing Trainee Skills – LPC, GDL and MA Law - London Moorgate campus Postgraduate
    Wed, 26 Feb '20
  • University of East Anglia
    PGCE Open day Postgraduate
    Sat, 29 Feb '20

Do you get study leave?

Yes- I like it (119)
Yes- I don't like it (10)
No- I want it (57)
No- I don't want it (16)

Watched Threads

View All
My Feed