Issue
I want to append values to a key in a dictionary, where the values are actually lists. There could be one list, or there could be multiple lists.
The current way I'm trying to do it, the new list I try to add on just overwrites the last list. As far as I can tell, I can't append a list to a dict like so either:
my_dict.append(my_list))
Current code that overwrites each other:
urls[domain] = [uri, datecreated]
Is this possible? If so, how?
Sample results would look like this
print all keys and values in urls{}:
google.com : ['/helloworld', 2010-04-02], ['/apage/anotherpage', 2015-09-12]
microsoft.com : ['/awebpage', 2009-02-01], ['/bananas', 2015-12-24], ['/anothergreaturi', 205-12-10]
Solution
You can have each dictionary entry a list, so you append to that list.
>>> a = Linter("ahahah")
>>> a.write_file()
>>> a = dict()
>>> a["first"] = []
>>> a["first"].append([1,2,3])
>>> a["first"].append([4,5,6])
>>> a["second"] = []
>>> a["second"].append([7,8,9])
>>> a
{
'second':[
[7, 8, 9]
],
'first': [
[1, 2, 3],
[4, 5, 6]
]
}
but honestly, at some point just consider using classes?
Answered By - ljetibo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.