Updating Python Dictionary Values

  • BTylerWhite

    Default port

    Points: 1463

    Comments posted to this topic are about the item Updating Python Dictionary Values

  • Wolfram Kraus

    SSCrazy

    Points: 2150

    I think this is a bad solution, because you re-set the whole entry for key 1 again. Why not use the following?

    people[1]['last_name'] = 'Dactill'
  • dawryn

    SSCarpal Tunnel

    Points: 4769

    I agree it is bad practice.

    If this was intended as update showcase:

    people[1].update(last_name='Dactill')

    • This reply was modified 2 weeks, 1 day ago by  dawryn.
  • BTylerWhite

    Default port

    Points: 1463

    Thanks for the feedback!

    Wolfram Kraus wrote:

    I think this is a bad solution, because you re-set the whole entry for key 1 again. Why not use the following?

    people[1]['last_name'] = 'Dactill'

    I agree, it does seem somewhat counter-intuitive to pass the dictionary again associated with the first key as well as repeating the values. Programatically, this would also be more difficult to achieve. I shared that solution as I found it to be a bit more simpler.

    dawryn wrote:

    I agree it is bad practice.

    If this was intended as update showcase:

    people[1].update(last_name='Dactill')

    I like this solution a lot, thank you for sharing! There are almost infinite possibilities of alternate ways to achieve our desired outcome. This one certainly would have been easier to understand with knowledge of indexing dictionaries. The update demonstration would have been much easier without a nested dictionary in this case.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply