Updating Python Dictionary Values

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

  • 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 is bad practice.

    If this was intended as update showcase:

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

    • This reply was modified 4 years, 3 months ago by  dawryn.
  • 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 3 (of 3 total)

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