I have 14 dictionaries all containing the same keys of information (i.e. time, date etc.) , but varying values. I’m trying to build a function that will put together a sentence when the dictionary is listed as the argument in the function.
If I have a dictionary:
dic1 = {'Name': 'John', 'Time': 'morning'}
And I want to concatenate them to a string:
print 'Hello ' + dic1['Name']+', good ' + dic1['Time']+ '.'
How would I go about this?
*Note, sorry, this returns the error:
TypeError: can only concatenate list (not "str") to list
Using new style string formatting (python2.6 or newer):
As for your error, I can’t explain it with the code you’ve provided above. If you try to
__add__a string to a list, you’ll get that error:But adding a list to a string (which is what you would be doing with the example code if your dictionary had a list as a value) gives a slightly different error: