list of lists

Iwo Herka hi at iwoherka.eu
Sun Jul 22 09:04:21 EDT 2018


> Can you tell me how this works?

"results[0]" returns a list with two elements. Let's call it "pair"

    pair = results[0]
    # ['1', 0.99921393753233001]

Now, we can use regular sequence unpacking to retrieve first and second argument:
    
    a, b = pair

which is equivalent to this:

    a = pair[0]
    b = pair[1]

If you're not sure how many items you have in a list, you can use an asterisk operator:

    li = [1, 2, 3, 4]
    a, b, *c = li

which is equivalent to:

   a = li[0]
   b = li[1]
   c = li[2:]


​Iwo Herka
https://github.com/IwoHerka​

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐

On 22 July 2018 12:40 PM, Sharan Basappa <sharan.basappa at gmail.com> wrote:

> ​​
> 
> Thanks. This works in my example. Can you tell me how this works?
> 
> > You can simply unpack the inner list:
> > 
> >     a, b = results[0]
> >     
> > 
> > Iwo Herka
> > 
> > ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
> > 
> > On 22 July 2018 11:47 AM, Sharan Basappa sharan.basappa at gmail.com wrote:
> > 
> > > 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?
> > > 
> > > https://mail.python.org/mailman/listinfo/python-list
> 
> Thanks
> 
> 
> ----------
> 
> https://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list