# For loops in pythonWatch

Announcements
#1
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
0
1 year ago
#2
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.
Code:
```# 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):
oneDimensional.append(i);```
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.
Code:
```# 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
twoDimensional.append(oneDimensional)```
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:
https://snakify.org/lessons/two_dime..._lists_arrays/

Or here (more examples using the shortened list comprehension format too):
https://stackoverflow.com/questions/...rray-in-python
https://stackoverflow.com/questions/...trix-in-python
0
X

new posts
Back
to top
Latest
My Feed

### Oops, nobody has postedin the last few hours.

Why not re-start the conversation?

see more

### See more of what you like onThe 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
Sat, 29 Feb '20

### Poll

Join the discussion

#### Do you get study leave?

Yes- I like it (119)
58.91%
Yes- I don't like it (10)
4.95%
No- I want it (57)
28.22%
No- I don't want it (16)
7.92%