Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
di={'name':['a','b','c'],'sex':['f','m','f'],'age':[22,33,22]}
I have one dictionary and keys are name,sex,age. I have to create two more new key like
new_age and gender.
new_age is like whoever below 30 then age+5 else age+10
gender is like if m then male if f female
For your reference:
{'name':['a','b','c'],'sex':['f','m','f'],'age':[22,33,22],'gender':['female','male','female'],'new_age':[27,43,27}

What I have tried:

di={'name':['a','b','c'],'sex':['f','m','f'],'age':[22,33,22]}
I have one dictionary and keys are name,sex,age. I have to create two more new key like
new_age and gender.
new_age is like whoever below 30 then age+5 else age+10
gender is like if m then male if f female
For your reference:
{'name':['a','b','c'],'sex':['f','m','f'],'age':[22,33,22],'gender':['female','male','female'],'new_age':[27,43,27}
Posted
Updated 28-Mar-21 21:45pm

1 solution

Python
di={'name':['a','b','c'],'sex':['f','m','f'],'age':[22,33,22]}
new_age = []
for age in di['age']:
    if age < 30:
        new_age.append(age + 5)
    else:
        new_age.append(age + 10)
di['new_age'] = new_age

See 5. Data Structures — Python 3.9.2 documentation[^].

[edit]
I notice that i already explained this to you the other day at How to order the values in dictionaries in Python[^]. You need to spend more time studying the documentation.
[/edit]
 
Share this answer
 
v3
Comments
Maciej Los 29-Mar-21 5:11am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900