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:
- How can I use a dictionary to do multiple search-and-replace operations? [duplicate]
- Adding a element in dictionary which consist of dictionaries.
- How do I merge two dictionaries in a single expression (take union of dictionaries)?
- Iterating over dictionaries using ‘for’ loops
- Delete an element from a dictionary
- How to copy a dictionary and only edit the copy
- How to merge multiple dicts with same key or different key?
- Rename a dictionary key
- Modifying a Python dict while iterating over it
- What’s a correct and good way to implement __hash__()?
- How to keep track of class instances?
- Safe method to get value of nested dictionary
- How to convert an XML string to a dictionary?
- How to sum dict elements
- How do I format a string using a dictionary in python-3.x?
- How to add multiple values to a dictionary key in python?
- Thread Safety in Python’s dictionary
- python filter list of dictionaries based on key value
- Extract subset of key-value pairs from dictionary?
- String to Dictionary in Python [duplicate]
- sort dict by value python [duplicate]
- Setting a value in a nested Python dictionary given a list of indices and value
- Efficient way to remove keys with empty strings from a dict
- How to convert a list to a dictionary with indexes as values?
- How can I avoid “RuntimeError: dictionary changed size during iteration” error?
- Why updating “shallow” copy dictionary doesn’t update “original” dictionary? [duplicate]
- “unpacking” a passed dictionary into the function’s name space in Python?
- How to detect if any element in a dictionary changes?
- How do I re-map python dict keys
- How do I merge dictionaries together in Python?