3. Make a List - Syntax confusion


#1


The code works fine, I just don't understand why.

I thought that the contents of a list had to be inside the square brackets. How does adding the * 5 outside of the list result in 5 letter 'O's being printed back inside the list?

The working code reads to me as: "Create a list containing only the letter O. Do this five times."

Why doesn't this work instead?: board.append(["O" * 5])



#2

What it will give is,

['OOOOO']

Python lists are extendable by concatenation or multiplication.

['O'] + ['O'] + ['O'] + ['O'] + ['O']

gives,

['O', 'O', 'O', 'O', 'O']

Using the multiplier shortens the concatenation to the simpler expression.


#3

I just started python a week ago and finally got to this step.

I've had to use this q and a to get past some exercises but this one... just makes me feel very dumb. did you come up with this code yourself? I can't imagine how people are solving these exercises just learning from this tutorial.

how has your experience been so far? did you have prior coding?


#4

The shorthand method may have been shared some years ago and spread in usage. With what you know, we can still build the board...

for row in range(5):
    board.append(['O', 'O', 'O', 'O', 'O'])

#5

I have no prior experience with coding. I've always been a bit intimidated by it because I was never good at math or holding multiple things in my memory at once while working through a problem.

I'm breezing through some parts of this and getting stuck on others. Most of the time when I run into a confusing part, it's due to poorly phrased or formatted instructions. The general difficulty with these coures is that the courses themselves cannot determine your current comfort level, or what concepts you need additional practice with before proceeding. A person generally needs a human guide for this, which I suppose is where the Pro subscription comes in handy.

I'm trying to go through these courses while also referring to additional third party info, and working through the Pro exercises to help reinforce what I've learned, through repetition.

I wish you the best of luck. :slight_smile:


#6

I think I understand it now. I had to rephrase it in my head, I guess. I was thinking that I was using the append function to append a list to a list, rather than appending a new item to the tail end of a list. Something along those lines, anyway.

Thanks!


#7

No, I looked on here for help because I didn't understand what the exercise wanted me to do with the range function, having only previously used that function in a very limited capacity.

I think it's fine to come on here looking for answers as long as we take the time to understand why they are correct. At this early stage, I'd rather spend an entire day agonizing over a single line of code than go on without understanding it fully.

There are definitely moments when the course will make you feel as if you are supposed to know how to do what it's asking you to do. Seek out additional practice or guidance. I've also revisited several old exercises to refresh my memory on some concepts.


#8

I'm very grateful for this response.

I was completely in the dark when they suggested to use the range()!

Thanks for your words of encouragement and insight. good luck to you too!

thank you mtf too!


#9

There are a lot of things to learn about lists. A good reference to keep handy is the docs on the Python List Class.

The Python Standard Library

Lists are introduced in the Sequence Types category. A quick search and other documentation for List will surely surface.

The most common way we see of adding to a list is list.append() but as we've seen above, it is not the only way.

my_list = []
my_list += ['new element']

print my_list    # ['new element']

The counterpart to .append is list.insert(), which inserts a new element at the front of the list. When we give an index as the second argument to this method, it will be inserted before that index (becoming bound to that index).

We can also add to the front of the list with concatenation...

my_list = ["newest element"] + my_list

print my_list    # ['newest element', 'new element']

The difference between direct concatenation and appending/inserting is that we can append value only and a new element is created for it using the methods, but must create a list element when concatenating.

Fairly soon you should come upon list slicing, which is a very powerful tool that will allow us to insert anywhere in a list.

my_list = my_list[0:1] + ['inserted element'] + my_list[1:]

print my_list    # ['newest element', 'inserted element', 'new element']

Again, we must create a list element of any length in order to do this.


#10

You forgot the 0 at the start. You need your code to look like this:
board = []

for i in range(0, 5):
board.append(["O"] * 5)


#11

Python defaults to zero as the starting value of a range if it is not specified.

range(5)  same as  range(0, 5)

#12

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.