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

David L Neil PyTutor at DancesWithMice.info
Thu Jan 30 21:24:03 EST 2020


On 31/01/20 12:10 PM, Panchanathan Suresh wrote:
> Thanks a lot Mats and David! I am not yet that familiar with data
> structures.

In which case the earlier suggestion of creating a 'clever class' was 
inappropriate for such a skill-level. Don't worry though, you'll get 
there...


> This statement indeed works: user_groups[user].append(group)

great!


> 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...

More-so for us because the code has (presumably) changed since last 
shown to us!

Please copy-paste the (relevant part of the) code from your editor/the 
REPL into future email msgs (plus any err.msgs, if necessary).


> Can we append to the list of dictionary values for a given key, using
> Append command?

Please review Built-in Types 
(https://docs.python.org/3/library/stdtypes.html)


You will find that a list is a sequence type, whereas a dict is a 
mapping type.

What does this mean? Lists/sequences are intended to be accessed 
serially, ie one element at a time, one after the other. So, if you have 
a play-list of tunes/songs, the intent is to listen to one after the other.

Whereas dicts/maps are direct-access structures. In this case, if each 
song has a name or an ID, the individual song may be accessed, using its 
name as a "key", without (first) accessing every other (previous) element.


Note also that a list is comprised of elements which are individual 
values (objects), eg

 >>> l = [ 1,
	"b",
	3.14159
	]
 >>> len( l )
3

but a dict's elements come in pairs - there must be a "key" and a 
"value", eg

 >>> d = { "question":Suresh",
		"answer":"dn"
		}
 >>> len( d )
2


So, to access an individual element, because the structures are 
different, the access mechanisms must necessarily also differ:

 >>> l[ 1 ]
'b'

- the list is a "sequence" therefore we "address" the individual element 
by its "index", its position within the sequence (an integer, starting 
from zero).

 >>> d[ "question" ]
'Suresh'

- the dict is a "mapping" thus we identify the individual element using 
its key - the key "maps" directly to a particular element with no 
reference to any other elements 'before' or 'after' it in the dict.


Right, let's talk about "append"! The word means quite literally 'add to 
the end'. End of what? If we were talking about a shopping list, then 
'the end' is the last entry - so we write the new item at 'the bottom' 
of the shopping list (and don't forget to take the shopping list with 
you to the store!). So l.append( ... ) has meaning!

What about a dictionary? Let's say you have a (book) dictionary, but 
it's getting out-of-date with modern jargon or may not have the 
complicated, fancy, words we use in computer jargon. Say you want to add 
a new word to that dictionary. Would you write it on the last page? No, 
better to add it 'in the right place'. So, if the new word was 
"meta-class", you would want to locate it somewhere after "meat" and 
before "moat". Thus d.append() has NO meaning - at the end? Instead try:

d[ "meta-class" ] = "Class inheriting from type"

We are now back to the earlier term: "direct access". In addition to 
"retrieving" data directly/by key, we are able to "store" data directly 
as well!

Good news! We don't have to work-out where in the dict Python stores 
things, as long as we know the keys we want to use!


So, there are some "methods" which can be used with both lists and 
dicts, but there are others which can only be used with lists and not 
dicts, and yet more which can be used with dicts but not lists. 
Accordingly, it follows that lists have a particular purpose and dicts 
have another. (the earlier web.ref lists methods for each, and more)

Just the same as a Green-billed Coucal and a Kiwi are both birds (not 
quite data structures). Both have wings and thus a flap 'method'. So, 
you'd think they could both fly. Not true! Different purposes, different 
lives, different islands!


NB the descriptions (above) are just that. The contents of lists can be 
much more complex - and often are; plus the rules about what we can use 
as "keys" for a dict, are significantly more involved (for example).


Have you spotted Alan's tutorial, or looked at Python courses on 
coursera.org or edx.org? (all free-access!) They might help you to learn 
under-pinnings like data structures, as well as Python itself...
-- 
Regards =dn


More information about the Tutor mailing list