How to create a new string by combining values corresponding to keys in several dictionaries in Python?

python merge list of dictionaries by key
write a python program to combine two dictionary adding values for common keys.
add multiple values to same key in dictionary python
python dictionary add value to existing key
python merge dictionaries with same keys
python merge dictionaries with common keys
python dictionary sum values with same key
python add multiple items to dictionary

I have two dictionaries:

time = {'JAN':'A','FEB':'B','MAR':'C','APR':'D','MAY':'E','JUN':'F','JUL':'H'}


and a table consisting of one single column where bond names are contained:


I need to replace the name with a string of the following format: EUA21 where the first two letters are the corresponding value to the currency key in the dictionary, the next letter is the value corresponding to the month key and the last two digits are the year from the name.

I tried to split the name using the following code:

bond_names['Names']=bond_names['Names'].apply(lambda x: x.split('.'))

but I am not sure how to proceed from here to create the string as I need to search the dictionaries at the same time for the currency and month extract the values join them and add the year from the name onto it.

This will give you a list of what you need:

time = {'JAN':'A','FEB':'B','MAR':'C','APR':'D','MAY':'E','JUN':'F','JUL':'H'}


bond_names = {'Names':['Bond.USD.JAN.21','Bond.USD.MAR.25','Bond.EUR.APR.22','Bond.HUF.JUN.21','Bond.HUF.JUL.23','Bond.GBP.JAN.21']}

result = []

for names in bond_names['Names']:
    bond = names.split('.')
    result.append(currency[bond[1]] + time[bond[2]] + bond[3])


Python, Given two dictionary, the task is to combine the dictionaries such that we get the added values for common keys in resultant dictionary. Example: Input: dict1  Python | Merge key value lists Sometimes, while working with lists, we can come forward with a problem in which we need to perform the merge function in which we have the key list and need to create dictionary mapping keys with corresponding value in other list.

You can do that like this:

import pandas as pd

time = {'JAN':'A','FEB':'B','MAR':'C','APR':'D','MAY':'E','JUN':'F','JUL':'H'}
currency = {'USD':'US','EUR':'EU','GBP':'GB','HUF':'HF'}
bond_names = pd.DataFrame({'Names': ['Bond.USD.JAN.21', 'Bond.USD.MAR.25', 'Bond.EUR.APR.22', 'Bond.HUF.JUN.21', 'Bond.HUF.JUL.23', 'Bond.GBP.JAN.21']})
bond_names['Names2'] = bond_names['Names'].apply(lambda x: currency[x[5:8]] + time[x[9:12]] + x[-2:])
# 0    USA21
# 1    USC25
# 2    EUD22
# 3    HFF21
# 4    HFH23
# 5    GBA21
# Name: Names2, dtype: object

Python, Dictionary is a collection which is unordered, changeable and indexed. In Python , dictionaries are written with curly brackets, and they have keys and values. In this article we will discuss different ways to merge two or more dictionaries. Also, handle scenarios where we need to keep the values of common keys instead of overwriting them.

With extended regex substitution:

In [42]: bond_names['Names'].str.replace(r'^[^.]+\.([^.]+)\.([^.]+)\.(\d+)', lambda m: '{}{}{}'.format(curre
    ...: ncy.get(,, time.get(,,                    
0    USA21
1    USC25
2    EUD22
3    HFF21
4    HFH23
5    GBA21
Name: Names, dtype: object

Associating Multiple Values with Each Key in a Dictionary, By nature, a dictionary is a one-to-one mapping, but it's not hard to make it one-to -many—in other words, to make one key map to multiple values. There are two  I have below 2 dictionaries that I want to merge. I want to merge on the same keys and I want to keep the values of both the dictionary. I used dict1.update(dict2) but that replaced the values from 2nd to 1st dictionary.

You can try this :

import pandas as pd

time = {'JAN':'A','FEB':'B','MAR':'C','APR':'D','MAY':'E','JUN':'F','JUL':'H'}


bond_names['Names']=bond_names['Names'].apply(lambda x: x.split('.'))

for idx, bond in enumerate(bond_names['Names']):
    currencyID = currency.get(bond[1])
    monthID = time.get(bond[2])
    yearID = bond[3]
    bond_names['Names'][idx] = currencyID + monthID + yearID


0  USA21 
1  USC25  
2  EUD22 
3  HFF21  
4  HFH23  
5  GBA21

How to Merge two or more Dictionaries in Python ? –, Both dictionaries has a common key 'Sam' with different values. Now let's Now merge the contents of dict1 and dict2 to a new dictionary dict3 i.e.. # Merge Therefore, a new dictionary is created that contains the data from both the dictionaries. Both dict1 Python: Check if any string is empty in a list? First of all, you have to create two empty lists keysList = [] and valuesList = [] for storing the keys and values respectively. After that, while iterating the dictionary items (dis.items ())using the for loop and appending the keys (keysList.append (keys)) and values (valuesList.append (values)) inside the keysList and valuesList.

20. Dictionaries, All of the compound data types we have studied in detail so far — strings, lists, and dictionary named eng2sp; the other assignments add new key:value pairs to the Python uses complex algorithms, designed for very fast access, to determine Another way to create a dictionary is to provide a list of key:value pairs using  In Python, there are a few ways to concatenate – or combine – strings. The new string that is created is referred to as a string object. Obviously, this is because everything in Python is an object – which is why Python is an objected-oriented language. In order to merge two strings into a single object, you may use the “+” operator.

Dictionaries in Python – Real Python, You can define a dictionary by enclosing a comma-separated list of key-value pairs in defines a dictionary that maps a location to the name of its corresponding to an existing dictionary is simply a matter of assigning a new key and value: several of the immutable types you are familiar with—integer, float, string, and  switch keys and values in dictionary python (11) I receive a dictionary as input, and would like to to return a dictionary whose keys will be the input's values and whose value will be the corresponding input keys. Values are unique. For example, say my input is:

Python Dictionary, Learn to create a Dictionary in Python, add remove access and change dictionary items, merge dictionaries, iterate through a dictionary, check if key exists, You can create a dictionary by placing a comma-separated list of key:value You can use any object of immutable type as dictionary keys – such as numbers, strings,  Going From a List to a String in Python With.join () There is another, more powerful, way to join strings together. You can go from a list to a string in Python with the join () method. The common use case here is when you have an iterable—like a list—made up of strings, and you want to combine those strings into a single string.

  • please see above added it as code thanks
  • Post your expected output as well please (not as an image)
  • Is the input Bond.USD.FEB.21 supposed to become BondUSB21 in the output? Or do you want the output to be USB21, Bond.US.B.21, or something else? https: