Python add dictionary to the list?

Good evening,
I want to add dictionary to the list, but after adding my list is 'None'.

#!/usr/bin/env python3

a = []
res = a.append({'id':'1', 'article':'name', 'url':'a.io', 'price':'1000'})
print(res)
April 4th 20 at 00:27
2 answers
April 4th 20 at 00:29
Solution
  • Method append() from the type list returns nothing, as it is logical. It list method and this method is called "add" -- what should it return? Nothing;
  • To refer to anything in Python provides object-single None (on Russian is translated - "nothing").
Thank you - brendon_Anders commented on April 4th 20 at 00:32
@brendon_Anders, always please.

If some of the answers helped, they should be noted by decision) - donald95 commented on April 4th 20 at 00:35
April 4th 20 at 00:31
a = []
res = a
Res. append({'id':'1', 'article':'name', 'url':'a.io', 'price':'1000'})
print(res)
Why the extra gesture with the variable a, if you can just res be declared as an empty dictionary ?

res = []
Res. append({'id':'1', 'article':'name', 'url':'a.io', 'price':'1000'})
print(res)
- kailyn commented on April 4th 20 at 00:34
@kailyn, I am a Creator, I see) - Liliane_Wiso commented on April 4th 20 at 00:37
@Liliane_Wiso, no, it's the author of a woodpecker. - Elda_Brek commented on April 4th 20 at 00:40
@kailyn, then you can immediately res a list with a dictionary inside:
res = [{'id': '1', 'article': 'name', 'url': 'a.io', 'price': '1000'}]
- juliet_Dickens2 commented on April 4th 20 at 00:43
Can a dictionary contain a list?

#!/usr/bin/env python3

a = [ 
 {'www0': [{'id':'1', 'article_name':'laptop', 'url':'http://', 'price':'100'}] },
 {'www1': [{'id':'1', 'article_name':'laptop', 'url':'http://', 'price':'200'}] }
]

print( a[0]['www0'].append({'id':'3', 'article_name':'laptop', 'url':'http://', 'price':'300'}))
- brendon_Anders commented on April 4th 20 at 00:46
@brendon_Anders, Yes - Liliane_Wiso commented on April 4th 20 at 00:49
This example returns None - brendon_Anders commented on April 4th 20 at 00:52
@brendon_Anders, this Yes, a dictionary can contain a list. Actually already read at least one book about Python, the same error as in the first question. - Liliane_Wiso commented on April 4th 20 at 00:55
@juliet_Dickens2, Yes, I think this is the best option. - kailyn commented on April 4th 20 at 00:58

Find more questions by tags Python