How to access items of a list inside another list?

A forum for general discussion of the Python programming language.

How to access items of a list inside another list?

Postby p3475 » Wed Apr 09, 2014 8:50 am

hi guys, I'm new on this forum, I just begun to use python.
I tried everything before asking but I really don't understand what is wrong.
I solved a system of linear equations using sympy and the result looks like a list. The problem is that when I try to access a single value of the list I can only access the first (res[0]) and all of them appear. if I try to call a larger component (res[1]) I get the message that list index is out of range.
I thought that maybe is stored as a matrix of one column but I can't call res[0,1] because I get ' list indices must be integers, not tuple'
the shape of the result is (1,4) and every component is a symbol.
I'm sure that it is something really stupid but I really don't know what to do
thank you for any help
Last edited by Yoriz on Wed Apr 09, 2014 12:00 pm, edited 2 times in total.
Reason: First post lock, Changed Title.
p3475
 
Posts: 2
Joined: Wed Apr 09, 2014 8:06 am

Re: can't access to a result

Postby Mekire » Wed Apr 09, 2014 9:33 am

What is the output of:
Code: Select all
for element in res[0]:
    print(element)

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: can't access to a result

Postby p3475 » Wed Apr 09, 2014 11:35 am

I get the single results. But still, there is no way that I can call the single element of res.
But with the for cycle I can fill a new array and call from there.
Thank you
p3475
 
Posts: 2
Joined: Wed Apr 09, 2014 8:06 am

Re: can't access to a result

Postby Yoriz » Wed Apr 09, 2014 11:56 am

If the result is a list with a single item that is also a list you can access each item like this.
Code: Select all
res = [[1, 2, 3, 4]]
print res[0][0] # 1st item of the list
print res[0][1] # 2nd item of the list

result
Code: Select all
1
2
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 871
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Discussions

Who is online

Users browsing this forum: Google [Bot] and 3 guests