Reputation: 121
Right, I have a dictionary like this one:
my_dict = {'BAM': (1.985, 1.919), 'PLN': (4.509, 4.361),'SEK': (9.929, 9.609), 'CZK': (27.544, 26.544),
'NOK': (9.2471, 8.9071), 'AUD': (1.4444, 1.4004),
'HUF': (315.89, 307.09), 'GBP': (0.8639, 0.8399),
'HRK': (7.6508, 7.4208), 'RUB': (71.9393, 66.5393),
'USD': (1.0748, 1.0508), 'MKD': (62.11, 60.29),
'CHF': (1.0942, 1.0602), 'JPY': (121.83, 118.03),
'BGN': (1.979, 1.925), 'RSD': (124.94, 121.14),
'DKK': (7.5521, 7.3281), 'CAD': (1.4528, 1.4048)}
I need to write a function (my_dict, ["GBP", "USD", "RUB", "HRK", "HUF"]) that returns this:
GBP......0.8639......0.8399
USD......1.0748......1.0508
RUB.....71.9393.....66.5393
HRK......7.6508......7.4208
HUF....315.8900....307.0900
We are learning how to format string, and I have no idea how to approach this. Any help would be appreciated. It needs to be formatted exactly like this, with all the dots and stuff (without the empty space before GBP).
Upvotes: 0
Views: 71
Reputation: 508
One of the simpler ways to do what you're asking is to use a list comprehension:
def my_function(dict, list):
return ["{0}......{1}......{2}".format(item, dict[item][0], dict[item][1])
for item in list
if item in dict]
my_function
will return a list of currency......value......value
items. In fact, you don't even need to create a function:
strings = ["{0}......{1}......{2}".format(item, dict[item][0], dict[item][1])
for item in list
if item in dict]
If however you don't want to use a list comprehension, the same function could look like this:
def my_function(dict, list):
strings = []
for item in list:
if item in dict:
strings.append("{0}......{1}......{2}".format(item, dict[item][0], dict[item][1]))
return strings
Upvotes: 1