How to create a dictionary from a dictionary based on indexes
Question:
I will try to easily explain my problem. I have a dictionary that looks like this:
dic = {
'I2a1a': ['I2a1a2', 'I2a1a2a', 'I2a1a2a1a2'],
'I2a1b': ['I2a1b1a1a1b1a'],
'I2a1b2': ['I2a1b2a']
}
Based on this dictionary, when the length of the value is > 1 I want to create a new dictionary that will look like this:
new_dic = {
'I2a1a': 'I2a1a2',
'I2a1a2': 'I2a1a2a',
'I2a1a2a': 'I2a1a2a1a2',
'I2a1b': 'I2a1b1a1a1b1a',
'I2a1b2': 'I2a1b2a'
}
As we can see for the key 'I2a1a'
in the first dictionary, each value (except the last value) is a key in the new_dic
and the value associated with it is the index n+1
from the values of the key 'I2a1a'
of the first dictionary.
I have no idea where to start my script!
Answers:
I couldn’t understand very well what you are asking even because on the first key you didn’t add any index.
Anyway, I put down some code that you can edit and use for your needs:
dic = {'I2a1a': ['I2a1a2', 'I2a1a2a', 'I2a1a2a1a2'], 'I2a1b': ['I2a1b1a1a1b1a'], 'I2a1b2': ['I2a1b2a']}
dic2 = {}
for element in dic:
if len(dic[element]) >1:
i=0
while i<len(dic[element]):
key = element + (str(i))
dic2[key]=dic[element][i]
i +=1
else:
dic2[element] = dic[element][0]
print(dic2)
The output is:
{'I2a1a0': 'I2a1a2', 'I2a1a1': 'I2a1a2a', 'I2a1a2': 'I2a1a2a1a2', 'I2a1b': 'I2a1b1a1a1b1a', 'I2a1b2': 'I2a1b2a'
Often when you have no idea where to start, it helps to write down the steps you would take if you were to solve this problem without a computer. Then translate your algorithm to code. For instance:
- Iterate over
dic.items()
- For each
key
–value
pair, create a list containing the key, and then all elements of the value
list.
- Iterate over consecutive pairs (
current_elem
, next_elem
) in this new list
- In this iteration, the
current_elem
is the key for the new_dic
, and next_elem
is the value.
Now let’s write this as code:
dic = {'I2a1a': ['I2a1a2', 'I2a1a2a', 'I2a1a2a1a2'],
'I2a1b': ['I2a1b1a1a1b1a'],
'I2a1b2': ['I2a1b2a']}
new_dic = {}
for key, value in dic.items(): # 1.
new_elems = [key, *value] # 2.
for current_elem, next_elem in zip(new_elems[:-1], new_elems[1:]): # 3.
new_dic[current_elem] = next_elem # 4.
which gives the desired new_dic
:
{'I2a1a': 'I2a1a2', 'I2a1a2': 'I2a1a2a', 'I2a1a2a': 'I2a1a2a1a2',
'I2a1b': 'I2a1b1a1a1b1a',
'I2a1b2': 'I2a1b2a'}
I think you might want something like this (although it doesn’t fit exactly your explanation):
dic = {'I2a1a': ['I2a1a2', 'I2a1a2a', 'I2a1a2a1a2'],
'I2a1b': ['I2a1b1a1a1b1a'],
'I2a1b2': ['I2a1b2a']}
new_dic = {}
for key, val in dic.items():
# Get a plain list whose first element is the key, and the rest is the value
val = [key] + val
# Iterate the list by pairs
for new_key, new_val in zip(val, val[1:]):
new_dic[new_key] = new_val
print(new_dic)
This way we just flat the lists in the values.
I will try to easily explain my problem. I have a dictionary that looks like this:
dic = {
'I2a1a': ['I2a1a2', 'I2a1a2a', 'I2a1a2a1a2'],
'I2a1b': ['I2a1b1a1a1b1a'],
'I2a1b2': ['I2a1b2a']
}
Based on this dictionary, when the length of the value is > 1 I want to create a new dictionary that will look like this:
new_dic = {
'I2a1a': 'I2a1a2',
'I2a1a2': 'I2a1a2a',
'I2a1a2a': 'I2a1a2a1a2',
'I2a1b': 'I2a1b1a1a1b1a',
'I2a1b2': 'I2a1b2a'
}
As we can see for the key 'I2a1a'
in the first dictionary, each value (except the last value) is a key in the new_dic
and the value associated with it is the index n+1
from the values of the key 'I2a1a'
of the first dictionary.
I have no idea where to start my script!
I couldn’t understand very well what you are asking even because on the first key you didn’t add any index.
Anyway, I put down some code that you can edit and use for your needs:
dic = {'I2a1a': ['I2a1a2', 'I2a1a2a', 'I2a1a2a1a2'], 'I2a1b': ['I2a1b1a1a1b1a'], 'I2a1b2': ['I2a1b2a']}
dic2 = {}
for element in dic:
if len(dic[element]) >1:
i=0
while i<len(dic[element]):
key = element + (str(i))
dic2[key]=dic[element][i]
i +=1
else:
dic2[element] = dic[element][0]
print(dic2)
The output is:
{'I2a1a0': 'I2a1a2', 'I2a1a1': 'I2a1a2a', 'I2a1a2': 'I2a1a2a1a2', 'I2a1b': 'I2a1b1a1a1b1a', 'I2a1b2': 'I2a1b2a'
Often when you have no idea where to start, it helps to write down the steps you would take if you were to solve this problem without a computer. Then translate your algorithm to code. For instance:
- Iterate over
dic.items()
- For each
key
–value
pair, create a list containing the key, and then all elements of thevalue
list. - Iterate over consecutive pairs (
current_elem
,next_elem
) in this new list - In this iteration, the
current_elem
is the key for thenew_dic
, andnext_elem
is the value.
Now let’s write this as code:
dic = {'I2a1a': ['I2a1a2', 'I2a1a2a', 'I2a1a2a1a2'],
'I2a1b': ['I2a1b1a1a1b1a'],
'I2a1b2': ['I2a1b2a']}
new_dic = {}
for key, value in dic.items(): # 1.
new_elems = [key, *value] # 2.
for current_elem, next_elem in zip(new_elems[:-1], new_elems[1:]): # 3.
new_dic[current_elem] = next_elem # 4.
which gives the desired new_dic
:
{'I2a1a': 'I2a1a2', 'I2a1a2': 'I2a1a2a', 'I2a1a2a': 'I2a1a2a1a2',
'I2a1b': 'I2a1b1a1a1b1a',
'I2a1b2': 'I2a1b2a'}
I think you might want something like this (although it doesn’t fit exactly your explanation):
dic = {'I2a1a': ['I2a1a2', 'I2a1a2a', 'I2a1a2a1a2'],
'I2a1b': ['I2a1b1a1a1b1a'],
'I2a1b2': ['I2a1b2a']}
new_dic = {}
for key, val in dic.items():
# Get a plain list whose first element is the key, and the rest is the value
val = [key] + val
# Iterate the list by pairs
for new_key, new_val in zip(val, val[1:]):
new_dic[new_key] = new_val
print(new_dic)
This way we just flat the lists in the values.