Reputation:
question = []
question.append({'id' : 5, 'title' : 'First question', 'content' : 'Hi there'})
print("Question", question)
question.append({'username' : 'Ali'})
print("Question", question)
I would like to extend a list as if I was adding these items from stratch. The output of the print statement is:
Question [{'id': 5, 'title': 'First question', 'content': 'Hi there'}, {'username': 'Ali'}]
How should I extend the list so that the print statement outputs this:
Question [{'id': 5, 'title': 'First question', 'content': 'Hi there', 'username': 'Ali'}]
Update: In the real example I tried three approaches. I got these error messages.
question[-1].update({'username' : 'Ali'})
AttributeError: 'dict' object attribute 'update' is read-only
(I've also tried this before)
question["username"] = 'Ali'
TypeError: list indices must be integers or slices, not str
question[0]["username"] = 'Ali'
works fine.
Upvotes: 1
Views: 373
Reputation: 1014
What you are doing there is, actually adding a dictionary inside a list.
From what you have asked in the question you would like to add another variable to that dictionary so that it has a username
key.
If thats what you want, following is a better way to do it.
question = {} #this will define the dictionary
question['id'] = 5
question['title'] = 'First question'
question['content'] = 'Hi there'
And then add the username
value to that dictionary by
question['username'] = 'Ali'
Following line is also equivalent to the first 4 lines of the previous code
question = {'id' : 5, 'title' : 'First question', 'content' : 'Hi there'}
and then add the username
key to the dictionary by question['username'] = 'Ali
To answer what you have asked, (which doesnt seem like the right way of doing things)
Now you have a list with single element which is dictionary, so to add values to the dictionary, you need to refer the first element of the list as follows
Instead of using question.append({'username' : 'Ali'})
use question[0]['username'] = 'Ali'
What you did is actually added an element to the list question
and hence the output was what you have mentioned.
Upvotes: 0
Reputation: 7268
You can try:
>>> question[0]["username"] = "Ali"
>>> print(question)
[{'username': 'Ali', 'id': 5, 'content': 'Hi there', 'title': 'First question'}]
Upvotes: 0
Reputation: 771
Your purpose is adding a new field to your object
but you are trying to append a new element {'username' : 'Ali'}
to your questions
list. So you should first add field 'username': 'Ali'
to your object then add this object to your questions
list.
Hope this helps:
question = []
your_object = {'id' : 5, 'title' : 'First question', 'content' : 'Hi there'}
your_object['username'] = 'Ali'
question.append(your_object)
print("Question", question)
Upvotes: 0
Reputation: 1666
You have a list of dictionaries:
dict_list = [{'foo': 'bar'}]
You're calling dict_list.append
, which appends an item to the list
dict_list.append({'baz': 'bork'})
print dict_list # => [{'foo': 'bar'}, {'baz': 'bork'}]
If you want to update the original dictionary, then you should instead retrieve that specific dictionary, and then update that
dict_list = [{'foo': 'bar'}]
dict_list[0]['baz'] = 'bork' # we could also use dict_list[0].update here
print dict_list # => [{'foo': 'bar', 'baz': 'bork'}]
That said, it looks like you're trying to build some kind of structure that holds these questions for easy retrieval. Rather than using a list for this, it might be more sensible to use a dict-of-dicts, where the key of each question was its id, something like:
questions = {}
questions[5] = {'title' : 'First question', 'content' : 'Hi there'}
questions[5]['username'] = 'Ali'
Upvotes: 0
Reputation: 8378
question = {'id' : 5, 'title' : 'First question', 'content' : 'Hi there'}
# "extend" (=update the dictionary):
question['username'] = 'Ali'
Upvotes: 0
Reputation: 1580
I used this for your query. Not sure if this solves your problem:
question = []
question.append({'id' : 5, 'title' : 'First question', 'content' : 'Hi there'})
question[0]["username"] = "test"
print question
Output:
[{'content': 'Hi there', 'username': 'test', 'id': 5, 'title': 'First question'}]
Upvotes: 0