dict, ( dict ). , , - :
for key in cities:
new_key = key.replace(' ', '')
if new_key == key:
continue
cities[new_key] = d[key]
del cities[key]
Otherwise, if you do not require or even do not want to preserve your identity ( truncatedCities- this is just created dictand not the same as cities), you can, of course, use the understanding:
citiesTruncated = {key.replace(" ", ""): value for key, value in cities.items()}
Not that in the second solution, even if you get a new one dict, the values remain the same (not only equal) - this means that adding something to cities["Las Vegas"]will be added to citiesTruncated["LasVegas"], because they are the same.
source
share