Append data to a list within a dict

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tina I

    #1

    Append data to a list within a dict

    Hello group,

    Say I have the following dictionary:

    ListDict = {
    'one' : ['oneone' , 'onetwo' , 'onethree'],
    'two' : ['twoone' , 'twotwo', 'twothree'],
    'three' : ['threeone' , 'threetwo', threethree']}

    Now I want to append 'twofour' to the list of the 'two' key but I can't
    figure out how to that?
    Some pointers would be greatly appreciated.

    Thanks
    Tina
  • Michael Bentley

    #2
    Re: Append data to a list within a dict


    On Apr 14, 2007, at 12:39 AM, Tina I wrote:
    Say I have the following dictionary:
    >
    ListDict = {
    'one' : ['oneone' , 'onetwo' , 'onethree'],
    'two' : ['twoone' , 'twotwo', 'twothree'],
    'three' : ['threeone' , 'threetwo', threethree']}
    >
    Now I want to append 'twofour' to the list of the 'two' key but I
    can't
    figure out how to that?
    Some pointers would be greatly appreciated.
    ListDict['two'].append('twofou r')

    But you'll have to insert the missing single quote before
    "threethree " first.

    hope this helps,
    Michael

    Comment

    • Paul Rubin

      #3
      Re: Append data to a list within a dict

      Tina I <tinaweb@bestem selv.comwrites:
      ListDict = {
      'one' : ['oneone' , 'onetwo' , 'onethree'],
      'two' : ['twoone' , 'twotwo', 'twothree'],
      'three' : ['threeone' , 'threetwo', threethree']}
      >
      Now I want to append 'twofour' to the list of the 'two' key but I
      can't figure out how to that?
      Is this a class exercise? Hint:
      1) figure out how to access the list of the 'two' key
      2) append 'twofour' to it.

      Comment

      • 7stud

        #4
        Re: Append data to a list within a dict

        On Apr 13, 11:39 pm, Tina I <tina...@bestem selv.comwrote:
        Hello group,
        >
        Say I have the following dictionary:
        >
        ListDict = {
        'one' : ['oneone' , 'onetwo' , 'onethree'],
        'two' : ['twoone' , 'twotwo', 'twothree'],
        'three' : ['threeone' , 'threetwo', threethree']}
        >
        Now I want to append 'twofour' to the list of the 'two' key but I can't
        figure out how to that?
        Some pointers would be greatly appreciated.
        >
        Thanks
        Tina

        ListDict["two"] returns the list. So you can do this:

        lst = listDict["two"]
        lst.append("two four")

        or you can do it directly like this:

        ListDict["two"].append("twofou r")


        Comment

        • Michael Bentley

          #5
          Re: Append data to a list within a dict


          On Apr 14, 2007, at 12:51 AM, Paul Rubin wrote:
          Is this a class exercise? Hint:
          1) figure out how to access the list of the 'two' key
          2) append 'twofour' to it.
          damn.

          Comment

          • Tina I

            #6
            Re: Append data to a list within a dict

            Paul Rubin wrote:
            Tina I <tinaweb@bestem selv.comwrites:
            >ListDict = {
            >'one' : ['oneone' , 'onetwo' , 'onethree'],
            >'two' : ['twoone' , 'twotwo', 'twothree'],
            >'three' : ['threeone' , 'threetwo', threethree']}
            >>
            >Now I want to append 'twofour' to the list of the 'two' key but I
            >can't figure out how to that?
            >
            Is this a class exercise? Hint:
            1) figure out how to access the list of the 'two' key
            2) append 'twofour' to it.
            He he... at the age of 40 I'm well beyond school work ;)
            But thanks anyway

            Tina

            Comment

            • Tina I

              #7
              Re: Append data to a list within a dict

              Michael Bentley wrote:
              >
              On Apr 14, 2007, at 12:39 AM, Tina I wrote:
              >
              >Say I have the following dictionary:
              >>
              >ListDict = {
              >'one' : ['oneone' , 'onetwo' , 'onethree'],
              >'two' : ['twoone' , 'twotwo', 'twothree'],
              >'three' : ['threeone' , 'threetwo', threethree']}
              >>
              >Now I want to append 'twofour' to the list of the 'two' key but I can't
              >figure out how to that?
              >Some pointers would be greatly appreciated.
              >
              ListDict['two'].append('twofou r')
              >
              But you'll have to insert the missing single quote before "threethree "
              first.
              >
              hope this helps,
              Michael
              Great! Thanks!
              And the missing singlequote was just a typo, my actual dictionary is way
              bigger so I just made up this as an example.

              Tina

              Comment

              • Hendrik van Rooyen

                #8
                Re: Append data to a list within a dict

                "Tina I" <ti,,eb@be...el v.comwrote:

                Hello group,
                >
                Say I have the following dictionary:
                >
                ListDict = {
                'one' : ['oneone' , 'onetwo' , 'onethree'],
                'two' : ['twoone' , 'twotwo', 'twothree'],
                'three' : ['threeone' , 'threetwo', threethree']}
                >
                Now I want to append 'twofour' to the list of the 'two' key but I can't
                figure out how to that?
                Some pointers would be greatly appreciated.
                >
                Just do it:

                ListDict['two'].append('twofou r')

                This works because ListDict['two'] is a list...

                - Hendrik

                Comment

                • Alex Martelli

                  #9
                  Re: Append data to a list within a dict

                  Tina I <tinaweb@bestem selv.comwrote:
                  ...
                  He he... at the age of 40 I'm well beyond school work ;)
                  Why would that be? My wife's over 40, yet she's a student (currently at
                  Stanford -- they were overjoyed to admit her, with lot of life
                  experience as well as previous studies, apparently). She's not taking
                  elementary courses on Python (having co-written a book on Python,
                  tech-reviewed a few, and having been the first woman coopted as a member
                  of the PSF:-), but she _has_ been taken equivalent ones on Java and C++
                  (languages she didn't previously know), as well as calculus,
                  neurophysiology , and other strange things that are part of the "Symbolic
                  Systems" studies.

                  Down with ageism!-)


                  Alex

                  Comment

                  • Tina I

                    #10
                    Re: Append data to a list within a dict

                    Alex Martelli wrote:
                    Tina I <tinaweb@bestem selv.comwrote:
                    ...
                    >He he... at the age of 40 I'm well beyond school work ;)
                    >
                    Why would that be? My wife's over 40, yet she's a student (currently at
                    Stanford -- they were overjoyed to admit her, with lot of life
                    experience as well as previous studies, apparently). She's not taking
                    elementary courses on Python (having co-written a book on Python,
                    tech-reviewed a few, and having been the first woman coopted as a member
                    of the PSF:-), but she _has_ been taken equivalent ones on Java and C++
                    (languages she didn't previously know), as well as calculus,
                    neurophysiology , and other strange things that are part of the "Symbolic
                    Systems" studies.
                    >
                    Down with ageism!-)
                    >
                    >
                    Alex
                    >
                    It's not really ageism, just how I feel *my self* about going back to
                    school. I'm not done learning though, it's just that I can give my self
                    the luxury of learning whatever I want, just what I want at the pace
                    that I want. And since I happen to like computers that's mostly what I
                    concentrate about. Right now I'm hooked on Python, but also messing
                    about with PHP and Javascript. Tried some Java but didn't like it...
                    Threw out Windows a couple of years ago and have spent quite some time
                    learning the ins and outs of Linux.

                    So even if I probably will never go back to school I'm looking forward
                    to many many years learning new and exciting things :)

                    Tina
                    (Sorry for the way OT post. I just couldn't stop my own ramble ;)

                    Comment

                    Working...