#!/usr/bin/env python # coding: utf-8 # # Advanced Dictionary Topics # built-in ** zip() ** function # - all builtin functions are listed here with examples: https://docs.python.org/3/library/functions.html # In[ ]: help(zip) # In[ ]: zdata = zip([1, 2, 3], ('a', 'b', 'c')) # In[ ]: print(zdata) # In[ ]: alist = list(zdata) print(alist) # In[ ]: adict = dict(alist) print(adict) # ## exercise # Create a dict that maps lowercase alphabets to integers, e.g., a maps to 1, b maps to 2, ..., z maps to 26 and print it # In[ ]: import string lettersToDigits = dict(zip(string.ascii_lowercase, range(1, 27))) # In[ ]: print(lettersToDigits) # ## Ordered dictionary # - defined in collections module # In[ ]: from collections import OrderedDict bdict = OrderedDict() # In[ ]: bdict['z'] = 26 bdict['a'] = 1 # In[ ]: print(bdict) # ## exercise # Create a dict that maps lowercase alphabets to their corresponding ASCII values , e.g., a maps to 97, b maps to 98, ..., z maps to 122 and print the dictionary in alphabetical order # In[ ]: import string lettersToDigits = dict(zip(string.ascii_lowercase, range(ord('a'), ord('z')+1))) # In[ ]: print(lettersToDigits) # In[ ]: