Python Forum
create empty sets for keys in a dictionary and add values to the set
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
create empty sets for keys in a dictionary and add values to the set
#2
collections.defaultdict can help.

from collections import defaultdict

setdict = defaultdict(set)
setdict[42].add(1337)
setdict[0].add("Foo")

print(setdict)
naughtysensei likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: create empty sets for keys in a dictionary and add values to the set - by DeaD_EyE - Nov-03-2020, 08:32 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how can i create a dictionary of dictionaries from a file Astone 2 2,287 Oct-26-2020, 02:40 PM
Last Post: DeaD_EyE
  Why is this function printing the number of keys in a dictionary? mafr97 5 3,099 Sep-17-2019, 06:19 AM
Last Post: perfringo
  Accessing nested keys and values in Dictreader cartographer72 2 2,419 Mar-08-2019, 03:55 AM
Last Post: cartographer72
  Take use input in sets and Dictionaries usman 4 3,214 Nov-18-2018, 01:54 PM
Last Post: usman
  Issues with Inserting Values into an Empty List with a While Loop TommyMer 2 3,801 Sep-12-2018, 12:43 AM
Last Post: TommyMer
  Looping to Create Nested Dictionary gngu2691 10 33,634 Jun-22-2018, 04:11 PM
Last Post: anickone
  logic comparater based on dictionary values ijosefson 3 3,271 Oct-16-2017, 06:04 AM
Last Post: Mekire

Forum Jump:

User Panel Messages

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