#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    10
    Rep Power
    0

    Scoundrel.popitem()


    i'm new to studying python and I admit I'm not far into my learning at all, but I want to keep up with it and something of an example confused me. I remember that the popitem method removes the last item of a sequence. My book provided the example:
    Code:
    scoundrel={'name':'person','position':'title'}
    key, value = scoundrel.popitem()
    I understand that key and value are now assigned here, but what I don't understand is why calling scoundrel produces 'name':'person'.

    Does popitem directly modify a list or tuple or what have you, or create a new copy? In either case, why did popitem remove the first index in scoundrel assignment, but when I tried
    Code:
    key,value=scoundrel.popitem(1)
    , I got a traceback error?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    scoundrel={'name':'person','position':'title'}

    Curly brace syntax makes scoundrel a dictionary. dict objects are unordered. scoundrel.popitem() removes an item from the dictionary as a (key, value) tuple. You do not know ahead of time which item will be removed. The dict popitem method reduces the len of the dictionary by 1.

    A python list has a pop method which removes and returns the object at the tail of the list.

    Immutable (unchanging) tuples do not have a pop method.

    The python set type has a pop method. Like a dict object, a set is unordered. The pop method returns some object from the set and reduces the set len by 1.

    calling scoundrel produces 'name':'person'.
    dict objects are not callable. You cannot call scoundrel. "Calling scoundrel" means exactly scoundrel() . Nowhere did you show scoundrel() . dict objects have callable methods, such as popitem.

    key,value=scoundrel.popitem(1)
    popitem bound to scoundrel takes no arguments. You gave it one.


    Instead of writing all this, perhaps I should have just shown you this neat trick which you can use in the interpreter:
    Code:
    >>> help({}.popitem)
    Help on built-in function popitem:
    
    popitem(...)
        D.popitem() -> (k, v), remove and return some (key, value) pair as a
        2-tuple; but raise KeyError if D is empty.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    10
    Rep Power
    0

    wow


    i had no idea help was so interactive, i took it merely as help() and that was that. i knew there was something about randomness with dictionary popitem, but I still managed to confuse popitem() with what a list would have done. the random assignment answers the rest, thanks again guy

IMN logo majestic logo threadwatch logo seochat tools logo