Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

248
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

0

You need a set not a dictionary,

l = ["a", "b", "c"]
d = set(l)
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda