address = "123 north anywhere street"
for word, initial in {"NORTH":"N", "SOUTH":"S" }.items():
address = address.replace(word.lower(), initial)
print address
nice and concise and readable too.
More Related Contents:
- can you write a str.replace() using dictionary values in Python?
- How to name a file using a value in a list in a dictionary?
- I am trying to modify a dictionary value using the exec function
- Python list of dictionaries search
- Replace values in a pandas series via dictionary efficiently
- Why is dictionary ordering non-deterministic?
- Object of custom type as dictionary key
- Get the key corresponding to the minimum value within a dictionary
- Error: ” ‘dict’ object has no attribute ‘iteritems’ “
- How does the key argument in python’s sorted function work?
- Merging dictionary value lists in python
- Convert a python dict to a string and back
- How can I use if/else in a dictionary comprehension?
- python groupby behaviour?
- Recursively access dict via attributes as well as index access?
- Is there anything faster than dict()?
- Add a new item to a dictionary in Python [duplicate]
- How to iterate through a list of dictionaries in Jinja template?
- Python: Reducing memory usage of dictionary
- Remove an item from a dictionary when its key is unknown
- PySpark create new column with mapping from a dict
- Is self.__dict__.update(**kwargs) good or poor style?
- Creating a dictionary with list of lists in Python
- dataframe to dict such that one column is the key and the other is the value [duplicate]
- Python dictionary comprehension using locals() gives KeyError
- Mapping dictionary value to list
- How to change all the dictionary keys in a for loop with d.items()?
- How can I combine dictionaries with the same keys?
- How to sort a list of dictionaries by a value of the dictionary in Python?
- How to create a nested dictionary from a list in Python?