list of lists

Sharan Basappa sharan.basappa at gmail.com
Sun Jul 22 08:53:16 EDT 2018


On Sunday, 22 July 2018 18:15:23 UTC+5:30, Frank Millman  wrote:
> "Sharan Basappa"  wrote in message 
> news:8e261f75-03f7-4f80-a516-8318dd138657 at googlegroups.com...
> >
> > I am using a third party module that is returning list of lists.
> > I am using the example below to illustrate.
> >
> > 1 results = [['1', 0.99921393753233001]]
> > 2 k = results[0]
> > 3 print k[0]
> > 4 print k[1]
> >
> > Assume the line 1 is what is returned.
> > I am assigning that to another list (k on line 2) and then accessing the 
> > 1st and 2nd element in the list (line 3 and 4).
> >
> > How can I access elements of 1 and 0.99 without assigning it to another 
> > list?
> >
> 
> As you say, results is a list of lists.
> 
> results[0] returns the first inner list. There could potentially be more 
> than one, in which case results[1] would return the second one. Use 
> len(results) to find out how many inner lists there are.
> 
> results[0][0] returns the first element of the first inner list.
> 
> results[0][1] returns the second element of the first inner list.
> 
> HTH
> 
> Frank Millman

Thanks. I initially thought about this but did not know if this is legal syntax.
The following is the updated code taking into account what you suggested and the previous posted.

results = [['1', 0.99921393753233001]]
k = results[0]
print k[0]
print k[1]

a,b = results[0]

print a
print b

x = results[0][0]
y = results[0][1]

print x
print y



More information about the Python-list mailing list