confused with list indexing

This is the place for queries that don't fit in any of the other categories.

confused with list indexing

Postby Wommbatt » Thu Nov 28, 2013 10:46 am

First off, I'm not sure how I'd even go about finding this answer in a search engine, so my apologies if it's already been answered somewhere.

So here we go. I have this generic list of tuples. (In the program things get added to the list later on.):
Code: Select all
skill_list = [
# tuple layout: name, level, amount of dice, sides
    ("heal", 1, 1, 6),
    ]


Now when I want to reference the whole tuple I call :
Code: Select all
>>> print(skill_list[0])
('heal', 1, 1, 6)

But now if I want just one of the pieces of the tuple I call:
Code: Select all
>>> print(skill_list[0][0])
heal

This all makes sense to me, and I have no issues with it.

But when I start using "for i in list" statements the syntax changes on me.
Code: Select all
>>> for i in skill_list:
   print([i[1]])
[1]

Why does it go from skill_list[0][0] to [i[1]], and why does it still have the [] around it?
And if I want to do math with it it becomes: [i[1]][0] + 5

Answers are appreciated
-Wommbatt
Wommbatt
 
Posts: 24
Joined: Thu Jun 13, 2013 2:15 pm

Re: confused with list indexing

Postby stranac » Thu Nov 28, 2013 10:53 am

Wommbatt wrote:But when I start using "for i in list" statements the syntax changes on me.
Code: Select all
>>> for i in skill_list:
   print([i[1]])
[1]

Why does it go from skill_list[0][0] to [i[1]], and why does it still have the [] around it?


list[0][0] is 'heal'(the first element of first tuple), and i[1] is 1(the second element of every tuple).
Those two are not the same.
It has [] around it because you put it inside []. I have no idea why you did that...

Wommbatt wrote:And if I want to do math with it it becomes: [i[1]][0] + 5

That's also because you put it inside [].
It should just be i[1] + 5
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1218
Joined: Thu Feb 07, 2013 3:42 pm

Re: confused with list indexing

Postby Wommbatt » Thu Nov 28, 2013 11:23 am

Let me try to give you an example from what I'm working on.
Code: Select all
skill_list = [
# tuple layout: name, level, amount of dice, sides
    ("heal", 1, 1, 6),
    ("shield", 1, 1, 4)
    ]

num = 1
for i in skill_list:
            # format: "1    ) Name, Cost"
            cost = [i[1]][0] * 50
            print(num, ")", str([i[0]])[2:-2], cost, "exp\n")
            num += 1


Produces this:
Code: Select all
>>>
1 ) heal 50 exp

2 ) shield 50 exp

>>>


If i try [i[1]][0] * 50 without the [1] I get: "1 ) heal ('heal', 1, 1, 6," repeated 50 times.
And without the [0] I get: "1 ) heal [1, " repeated 50 times.
This only seems to occur inside the "for i in list" loops

-Wommbatt
Wommbatt
 
Posts: 24
Joined: Thu Jun 13, 2013 2:15 pm

Re: confused with list indexing

Postby Kebap » Thu Nov 28, 2013 11:27 am

Solution: Change [i[1]] to i[1], or i[0] like this:

Code: Select all
>>> for i in skill_list:
   print(i[0])
heal


Do not remove the index after i, this is still needed. No need to put the result in a new list though.

edit: Oh my goodness... str([i[0]])[2:-2] ?!? This is also just i[0] ... I think you overreacted somewhere. Good you ask! :mrgreen:
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 400
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: confused with list indexing

Postby Wommbatt » Thu Nov 28, 2013 12:22 pm

Well I'll be. I thought that I had to have the whole thing in the [] when I used a for loop. Thanks, that clears that up lol

As for the str([i[0]])[2:-2] mess.... I had to get rid of the ["..."] somehow lol

Glad to know I was wasting my time haha. Now I can clean my code up a bit 8D

Thanks for the redirecting fellas,
-Wommbatt
Wommbatt
 
Posts: 24
Joined: Thu Jun 13, 2013 2:15 pm


Return to General Coding Help

Who is online

Users browsing this forum: Mekire, mk35 and 4 guests