How to fill in a dictionary with key and value from a string?

MRAB python at mrabarnett.plus.com
Fri Mar 30 16:37:15 EDT 2018


On 2018-03-30 21:13, C W wrote:
> Hello all,
> 
> I want to create a dictionary.
> 
> The keys are 26 lowercase letters. The values are 26 uppercase letters.
> 
> The output should look like:
> {'a': 'A', 'b': 'B',...,'z':'Z' }
> 
> I know I can use string.ascii_lowercase and string.ascii_uppercase, but how
> do I use it exactly?
> I have tried the following to create the keys:
> 
> myDict = {}
>          for e in string.ascii_lowercase:
>              myDict[e]=0
> 
> But, how to fill in the values? Can I do myDict[0]='A', myDict[1]='B', and
> so on?
> 
The uppercase equivalent of what's in "e" is "e.upper()". Does that help?

Alternatively, string.ascii_lowercase will give you the keys and 
string.ascii_uppercase will give you the values for a dict; use "zip" to 
make them into pairs (2-tuples) and then pass the list/generator into 
"dict":

dict(zip(string.ascii_lowercase, string.ascii_uppercase))



More information about the Python-list mailing list