• If a dictionary key has a Python list as its value!

    From Varuna Seneviratna@3:633/280.2 to All on Fri Mar 8 01:11:11 2024
    Reply-To: varuna.seneviratna@gmail.com

    If a dictionary key has a Python list as its value, you can read the values
    one by one in the list using a for-loop like in the following.

    d = {k: [1,2,3]}


    for v in d[k]:
    print(v)


    No tutorial describes this, why?
    What is the Python explanation for this behaviour?

    Varuna

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From MRAB@3:633/280.2 to All on Fri Mar 8 05:23:46 2024
    On 2024-03-07 14:11, Varuna Seneviratna via Python-list wrote:
    If a dictionary key has a Python list as its value, you can read the values one by one in the list using a for-loop like in the following.

    d = {k: [1,2,3]}


    for v in d[k]:
    print(v)


    No tutorial describes this, why?
    What is the Python explanation for this behaviour?

    If the value is a list, you can do list things to it.

    If the value is a number, you can do number things to it.

    If the value is a string, you can do string things to it.

    And so on.

    It's not mentioned in tutorials because it's not special. It just
    behaves how you'd expect it to behave.

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Mats Wichmann@3:633/280.2 to All on Fri Mar 8 05:29:07 2024
    On 3/7/24 07:11, Varuna Seneviratna via Python-list wrote:
    If a dictionary key has a Python list as its value, you can read the values one by one in the list using a for-loop like in the following.

    d = {k: [1,2,3]}


    for v in d[k]:
    print(v)


    No tutorial describes this, why?
    What is the Python explanation for this behaviour?

    Sorry... why is this a surprise? If an object is iterable, you can
    iterate over it.

    d = {'key': [1, 2, 3]}
    type(d['key'])
    <class 'list'>
    val = d['key']
    type(val)
    <class 'list'>
    for v in val:
    .... print(v)
    ....
    ....
    1
    2
    3





    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Stefan Ram@3:633/280.2 to All on Fri Mar 8 06:21:31 2024
    Varuna Seneviratna <varunaseneviratna@gmail.com> wrote or quoted:
    If a dictionary key has a Python list as its value, you can read the values >one by one in the list using a for-loop like in the following.
    d = {k: [1,2,3]}
    for v in d[k]:
    print(v)
    No tutorial describes this, why?
    What is the Python explanation for this behaviour?

    This is explained by extensionality: To find the behavior of
    "for v in ...", the only thing one needs to know about "..."
    is its value. You could just as well have written:

    l = d[ k ]
    for v in l:

    . l can be any iterable. It does not matter where it came from.
    It does not matter that it cam from a dictionary. There are thousand
    places where it could have come from, and no tutorial can name them
    all.

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: Stefan Ram (3:633/280.2@fidonet)