list of lists

Sharan Basappa sharan.basappa at gmail.com
Sun Jul 22 09:19:15 EDT 2018


On Sunday, 22 July 2018 18:34:41 UTC+5:30, Iwo Herka  wrote:
> > 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

Thanks a lot. I have seen this syntax earlier but did not understand this fully. Thanks a lot.


More information about the Python-list mailing list