How to add entries to a dictionary?

Asked

Viewed 16,229 times

2

How to add user-provided entries to a dictionary? For example:

Entree:

1 abcde
2 adecd
3 aaabb

The created dictionary would be:

dicionario = {'abcde' : 1, 'adecd' : 2, 'aaabb' : 3}

or

dicionario = {1 : 'abcde', 2 : 'adecd', 3 : 'aaabb'}

Is there any function similar to append() to use in dicinaries as in list? I say this because as entries will be provided by the user, you can’t keep adding input per input (dicionario['abcde'] = 1).

4 answers

3


You can use something like:

entrada1 = input("Por favor insira um numero: ")
dicionario[entrada1] = 1
  • As there will be several entries, I do not know how many will be exactly (since who will do it is the user) I can use a loop to go accepting the entries and creating the dictionary, correct?

  • Yes an input control mechanism works well.

  • I’ll try. Thank you.

2

For this example you are showing:

1 abcde  
2 adecd  
3 aaabb

As if the user entered the values separated that way you can do so:

user_input = input('1 abcde')# simulando a entrada do usuário  
user_input = user_input.split(' ')  
user_notes[user_input[0]] = user_input[1];
  • I don’t understand. Who uses the program must provide the entries as shown, (1 abcde) and the program must create a dictionary with these entries, using each entry as key/value (or value/key), that is, the entries must be inserted in a dictionary , in the same way as it would be in a list, where each entry would be inserted into the list as it was provided, using append(), for example.

1

d = {}
v = raw_input('valor: ')
d.update({len(d)+1:v})

Or put it in a bow:

d = {}
while True:
    v = raw_input('valor')
    if v == 'Q':
        break
    d.update({len(d)+1:v})
print d

1

The most practical way to attach a new index with a defined value

dicionario['abcde'] = 1

But I don’t understand what’s stopping him from using.

You commented:

I say this because as entries will be provided by the user, no you can keep adding input per input (dicionario['abcde'] = 1).

So another way to do it would be with the update method()

dicionario.update({'abcde':1})

Getting the data via raw cgi

import cgi
form = cgi.FieldStorage()

for key in form:
    dicionario[key] = form[key]

Getting the data via request.POST

for key in request.POST:
    dicionario[key] = request.POST[key]
  • I need to use raw_input or input so that the program user (not me, who is writing) enters the entries. It is an exercise to create a program that receives student grades 1, 2, 3, n students and then compare your answers with a feedback. The user (like a teacher) should only enter the "number" of the student and his answers, in format 1 abcde, for example. So I can’t do it that way.

  • as simply assign the input value to the variable..

  • I’ll try. Thank you.

  • so I think your question is how to iterate data from the GET/POST method.. I added examples..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.