Python Forum
Store a set in a dictionary's value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Store a set in a dictionary's value
#3
# a dict value that is a set
d['Cubs'] = set()
# adding members to the set
d['Cubs'].add(1907)
d['Cubs'].add(1908)
d['Cubs'].add(2016)
# display contents of the set
print(d['Cubs'])
This might be a nice place to use a defaultdict. If it defaults to a set, you don't have to create the set for each winner before adding to it.

from collections import defaultdict
d = defaultdict(set)
d['White Sox'].add(1906)
d['Cubs'].add(1907)
print(d['Cubs'])
Reply


Messages In This Thread
RE: Store a set in a dictionary's value - by bowlofred - Oct-04-2020, 06:16 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Read csv file, parse data, and store in a dictionary markellefultz20 4 4,743 Nov-26-2019, 03:33 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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