Can't figure out how to work with list indexes
Gamarleton

I'm trying to reference an element in a list and return the next element in the list as a string.
But it keeps telling me it's the wrong syntax.

I've looke the syntax up too, and all I could find was to write it like this:

listName[indexNumber]

But I just get SyntaxError: invalid syntax

Edit: I realized my [email protected]** had forgotten a :.
Now, I instead get: TypeError: list indices must be integers or slices, not str
I don't know how to do this.

You are viewing a single comment. View All
MrVoo

Fixed?

Gamarleton

@MrVoo
Well if you read my Edit, I now have a TypeError, I feel like the "Learn Python the Right Way" pdf is either full of overclass exercises, or very lacking of actual information on how to do specific things they ask us to in said exercises.

MrVoo

@Gamarleton That means the i in if d == comp[i] is a string

Gamarleton

@MrVoo But how? Isn't that just an index variable that goes from 0-X ?

MrVoo

@Gamarleton No, that's subtraction. Use a colon. coolList[2:5]

Gamarleton

@MrVoo Dude, that's not what I meant... I did not mean 0 minus X. I meant from 0 to X.
That's what the iteratable loop variable does, as I've been told? How would that suddenly be a string?
The i in comp[i] would just refer to the current nr of loops, right?

MrVoo

@Gamarleton Your iterating a list, every item is a string. If you want to iterate the length of the list, do for i in range(len(comp))

Gamarleton

@MrVoo Oh man I forgot about that. Well that explains it.
I used similar syntax in Java so I mixed the language syntax up a bit.

Edit: Although according to w3schools.com, it's actually supposed to be possible.
Although it's meant to look for that given value in the string.
Source: https://www.w3schools.com/python/python_for_loops.asp
Weird.