Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

249
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

0

You need a set not a dictionary,

l = ["a", "b", "c"]
d = set(l)
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda