[Tutor] week 10 synthesizing assignment

Alan Gauld alan.gauld at btinternet.com
Sat Apr 2 04:15:42 EDT 2016


On 02/04/16 08:22, Daniella Sapozhnikova wrote:
> I have a couple of questions.
> 1) If I wanted to combine two dictionaries into one, how would I go about
> doing so? (the .update method?)

I'm not sure what you mean by that. There are at kleat 2 ways to
interpret it.
Do you want to merge the data from both into a single dictionary? Or do
you want a dictionary with two elements, each of which is itself a
dictionary?

Both are possible but its not clear which you mean.

> 2) How can I make that combined dictionary have inner dictionaries with the
> inner values from the previous separate dictionaries?

This sounds like the second of the two options I asked about above but
again I'm not sure. What do you mean by the "inner values of the
previous separate dictionaries"?

The simple explanation is:

>>> dict1 = {1:'a', 2:'e'}
>>> dict2 = {1:'a', 2:'b'}
>>> firstTwo = { vowels:dict1, alphabet:dict2}

Now I can do

print firstTwo['vowels']
print firstTwo['alphabet']

or

if firstTwo['vowels'][0] == firstTwo['alphabet'][0]:
   print 'the first vowel is also the first letter'

HTH


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