Python Forum
Add elements to a Dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add elements to a Dictionary
#2
I'm not sure I understand the question. Does this work for you?
mydict['Rocky 3'] = -1
I am confused about "overwriting". Do you mean that you don't want to add a movie if it already exists in the dictionary, or do you mean you want to add the movie without it replacing the existing dictionary entry for that same movie? If the latter, that is not what dictionaries are for and you would be better off making a list of tuples (or named tuples).

Another possibility is that instead of mapping movie titles to a rating, you map the movie title to a list of ratings. If you have multiple ratings for the same movie you just append the rating to the list of ratings. I did something like that recently responding to a post about poker hands. I wanted to group cards by suit and by rank, so I subclassed dictionary to create keyed groups (lists).
class Grouper(dict):
    '''I am like a dictionary, but I can map multiple values to the same key'''
    def __init__(self, items=[]):
        super().__init__()
        for item in items:
            self[item[0]] = item[1]
 
    def __setitem__(self, key, value):
        group = self.get(key, [])
        group.append(value)
        super().__setitem__(key, group)
  
    def most_common(self):
        '''Return items sorted in decreasing order by count'''
        items = list(super().items())
        items.sort(key=lambda item: len(item[1]), reverse=True)
        return items

movies = Grouper((('Rocky',  5),  ('Rocky II', 4), ('Rocky',  3))
movies['Rocky'] would return a list of ratings for the movie: [5, 3]
Reply


Messages In This Thread
Add elements to a Dictionary - by muzikman - Sep-08-2021, 07:29 PM
RE: Add elements to a Dictionary - by deanhystad - Sep-08-2021, 07:31 PM
RE: Add elements to a Dictionary - by Yoriz - Sep-08-2021, 07:42 PM
RE: Add elements to a Dictionary - by muzikman - Sep-08-2021, 07:46 PM
RE: Add elements to a Dictionary - by perfringo - Sep-08-2021, 07:52 PM
RE: Add elements to a Dictionary - by snippsat - Sep-09-2021, 12:54 AM
RE: Add elements to a Dictionary - by naughtyCat - Sep-09-2021, 05:33 AM
RE: Add elements to a Dictionary - by deanhystad - Sep-09-2021, 06:39 AM
RE: Add elements to a Dictionary - by muzikman - Sep-09-2021, 12:40 PM
RE: Add elements to a Dictionary - by snippsat - Sep-09-2021, 07:03 PM
RE: Add elements to a Dictionary - by muzikman - Sep-09-2021, 07:36 PM
RE: Add elements to a Dictionary - by snippsat - Sep-10-2021, 03:08 PM
RE: Add elements to a Dictionary - by muzikman - Sep-10-2021, 03:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,226 May-17-2022, 11:38 AM
Last Post: Larz60+
  Sorting Elements via parameters pointing to those elements. rpalmer 3 2,646 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  Looping through dictionary and comparing values with elements of a separate list. Mr_Keystrokes 5 3,946 Jun-22-2018, 03:08 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020