Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

255
Views
Make Dictionary with only keys?

I need to make a dictionnary containing only keys.

I cannot use d.append() as it is not a list, neither setdefault as it needs 2 arguments: a key and a value.

It should work as the following:

d = {}

add "a":

d = {"a"}

add "b":

d = {"a", "b")

add "c" ...

#Final result is

d = {"a", "b", "c"}

What is the code I need to get this result? Or is it another solution? Such as making a list.

l = ["a", "b", "c"] # and transform it into a dictionnary: d = {"a", "b", "c"} ?
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

A dict with only keys is called a set.

Start with an empty set instead of a dictionary.

d = set()
d.add('a')
d.add('b')
d.add('c')

You can also create a set via a {} expression:

d = { 'a', 'b', 'c' }

Or using a list:

d = set(['a', 'b', 'c'])
over 4 years ago · Santiago Trujillo Report

0

That should do it:

l = ["a", "b", "c"]

d = {k:None for k in l}

As @Rahul says in the comments, d = {"a", "b", "c"} is not a valid dictionary definition since it is lacking the values. You need to have values assigned to keys for a dictionary to exist and if you are lacking the values you can just assign None and update it later.

over 4 years ago · Santiago Trujillo Report

0

You need a set not a dictionary,

l = ["a", "b", "c"]
d = set(l)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!