How to fill in a dictionary with key and value from a string?
bartc
bc at freeuk.com
Sat Mar 31 06:59:51 EDT 2018
On 30/03/2018 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
If the input string S is "cat" and the desired output is {'c':'C',
'a':'A', 't':'T'}, then the loop might look like this:
D = {}
for c in S:
D[c] = c.upper()
print (D)
Output:
{'c': 'C', 'a': 'A', 't': 'T'}
> But, how to fill in the values? Can I do myDict[0]='A', myDict[1]='B', and
> so on?
Yes, but the result will be {0:'A', 1:'B',...} for which you don't need
a dict; a list will do.
--
bartc
More information about the Python-list
mailing list