[Tutor] Input is Dictionary1, Output is Dictionary2 (using keys and values of Dictionary1)

Alan Gauld alan.gauld at yahoo.co.uk
Thu Jan 30 19:37:10 EST 2020


On 30/01/2020 23:10, Panchanathan Suresh wrote:

> But how can we use 'append' keyword for a dictionary? Is not append used
> only for appending to a List?
> Is it because Group is a already a List in the original dictionary? This is
> a bit confusing...
> 
> Can we append to the list of dictionary values for a given key, using
> Append command?

Yes, because the list inside the dictionary is still a list, no
different to any other list. so you use the dictionary key to get a
reference to the list and then call the lists append method.

Note that append is not a keyword it is a method of the list object.
A keyword is something reserved by the language, like 'for', 'while',
'def' etc. The method or function names can be overridden.

for example, you could, if you wished, create your own print() function

def print(s):pass

that would hide the normal print. Then you couldn't print anything(*)!
Not very useful, but possible(in Python3) because print is just a
function name not a keyword.

(*)Actually you can by referencing the builtin function
via the __builtins__ module. But you get the idea, I hope.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list