How to append to a dictionary

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Harlin Seritt

    #1

    How to append to a dictionary

    I have some code here:

    groups = {'IRISH' : 'green', 'AMERICAN' : 'blue'}

    I want to add another key: 'ITALIAN' : 'orange'

    How do I append this to 'groups'?

    Thanks,

    Harlin Seritt

  • Tim Chase

    #2
    Re: How to append to a dictionary

    > groups = {'IRISH' : 'green', 'AMERICAN' : 'blue'}[color=blue]
    >
    > I want to add another key: 'ITALIAN' : 'orange'
    >
    > How do I append this to 'groups'?[/color]

    groups['ITALIAN'] = 'orange'

    as described at



    -tkc



    Comment

    • Sybren Stuvel

      #3
      Re: How to append to a dictionary

      Harlin Seritt enlightened us with:[color=blue]
      > I have some code here:
      >
      > groups = {'IRISH' : 'green', 'AMERICAN' : 'blue'}
      >
      > I want to add another key: 'ITALIAN' : 'orange'
      >
      > How do I append this to 'groups'?[/color]

      groups['ITALIAN'] = 'orange'

      Sybren
      --
      The problem with the world is stupidity. Not saying there should be a
      capital punishment for stupidity, but why don't we just take the
      safety labels off of everything and let the problem solve itself?
      Frank Zappa

      Comment

      • Ben Finney

        #4
        Re: How to append to a dictionary

        "Harlin Seritt" <harlinseritt@y ahoo.com> writes:
        [color=blue]
        > groups = {'IRISH' : 'green', 'AMERICAN' : 'blue'}
        >
        > I want to add another key: 'ITALIAN' : 'orange'
        >
        > How do I append this to 'groups'?[/color]

        Dictionary items have no implicit sequence, so you don't "append" to
        one.

        Assigning any value to a key in the dictionary will create that key if
        it doesn't exist.
        [color=blue][color=green][color=darkred]
        >>> groups = {'IRISH': 'green', 'AMERICAN': 'blue'}
        >>> groups['ITALIAN'] = 'orange'
        >>> print groups[/color][/color][/color]
        {'IRISH': 'green', 'AMERICAN': 'blue', 'ITALIAN': 'orange'}


        Please enjoy your trip through the Python tutorial, working through
        the examples and understanding each one.

        <URL:http://docs.python.org/tut/>

        --
        \ "It is the responsibility of intellectuals to tell the truth |
        `\ and expose lies." -- Noam Chomsky |
        _o__) |
        Ben Finney

        Comment

        Working...