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

    From Varuna Seneviratna@varunaseneviratna@gmail.com to comp.lang.python on Thu Mar 7 19:41:11 2024
    From Newsgroup: comp.lang.python

    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
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From MRAB@python@mrabarnett.plus.com to comp.lang.python on Thu Mar 7 18:23:46 2024
    From Newsgroup: comp.lang.python

    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.
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Mats Wichmann@mats@wichmann.us to comp.lang.python on Thu Mar 7 11:29:07 2024
    From Newsgroup: comp.lang.python

    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




    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From ram@ram@zedat.fu-berlin.de (Stefan Ram) to comp.lang.python on Thu Mar 7 19:21:31 2024
    From Newsgroup: comp.lang.python

    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.
    --- Synchronet 3.20a-Linux NewsLink 1.114