0

I have this python dictionary myDict:

{'Age': {0: '39'}, 'DailyRate': {0: '903'}, 'DistanceFromHome': {0: '2'}, 'EnvironmentSatisfaction': {0: '1'}, 'HourlyRate': {0: '41'}, 'JobInvolvement': {0: '4'}, 'JobLevel': {0: '3'}, 'JobSatisfaction': {0: '3'}, 'MonthlyIncome': {0: '7880'}, 'MonthlyRate': {0: '2560'}, 'NumCompaniesWorked': {0: '0'}, 'PercentSalaryHike': {0: '18'}, 'RelationshipSatisfaction': {0: '4'}, 'StandardHours': {0: '80'}, 'TotalWorkingYears': {0: '9'}, 'TrainingTimesLastYear': {0: '3'}, 'YearsAtCompany': {0: '8'}, 'YearsInCurrentRole': {0: '7'}, 'YearsSinceLastPromotion': {0: '0'}, 'YearsWithCurrManager': {0: '7'}, 'MaritalStatus_': {0: '2'}, 'JobRole_': {0: '7'}, 'Gender_': {0: '1'}, 'EducationField_': {0: '1'}, 'Department_': {0: '2'}, 'BusinessTravel_': {0: '2'}, 'OverTime_': {0: '1'}, 'Over18_': {0: '1'}}

As you can see, if i get a one from above sample as below,

{'Age': {0: '39'}}

There is an additional 0 in front of the value 39. And this zero presents in every key-value pair.

How can I get rid of this 0, so it looks like this:

{'Age': '39'}

I tried this method, but it removes the whole key instead of the 0:

map(myDict.pop, ['Age',''])

Can someone please help me?

11
  • 2
    Perhaps you are looking for {k: v[0] for k, v in myDict.items()}? Commented Sep 21, 2020 at 18:42
  • Hi, what is k, v here? Commented Sep 21, 2020 at 18:43
  • That's not removing values, that's transforming values. Commented Sep 21, 2020 at 18:44
  • can you provide a complete answer? I am confused. Sorry for that. Commented Sep 21, 2020 at 18:44
  • your dict is wrong. becouse Age is a dict of ages. Commented Sep 21, 2020 at 18:45

4 Answers 4

6

You can use dictionary comprehension to solve this issue. Try doing:

new_dict = {key: value[0] for key, value in old_dict.items()}

Here, you iterate through each key, value pair in the dictiory and assign the key of the new dictionary to the key of the old dictionary. But the value becomes the 0th key value of the dictionary inside the dictionary.

For an example, the key starts at 'Age', so the first key of the new dictionary is 'Age'. The value however is {0: '39'}[0] which is '39'. So the first element of the dictionary is 'Age': '39'

Sign up to request clarification or add additional context in comments.

1 Comment

Great! I would appreciate it if you could accept the answer if this helped you :)
0

you can read it by the following code:

dict= {'Age': {0: '39'}, 'DailyRate': {0: '903'}, 'DistanceFromHome': {0: '2'}, 'EnvironmentSatisfaction': {0: '1'}, 'HourlyRate': {0: '41'}, 'JobInvolvement': {0: '4'}, 'JobLevel': {0: '3'}, 'JobSatisfaction': {0: '3'}, 'MonthlyIncome': {0: '7880'}, 'MonthlyRate': {0: '2560'}, 'NumCompaniesWorked': {0: '0'}, 'PercentSalaryHike': {0: '18'}, 'RelationshipSatisfaction': {0: '4'}, 'StandardHours': {0: '80'}, 'TotalWorkingYears': {0: '9'}, 'TrainingTimesLastYear': {0: '3'}, 'YearsAtCompany': {0: '8'}, 'YearsInCurrentRole': {0: '7'}, 'YearsSinceLastPromotion': {0: '0'}, 'YearsWithCurrManager': {0: '7'}, 'MaritalStatus_': {0: '2'}, 'JobRole_': {0: '7'}, 'Gender_': {0: '1'}, 'EducationField_': {0: '1'}, 'Department_': {0: '2'}, 'BusinessTravel_': {0: '2'}, 'OverTime_': {0: '1'}, 'Over18_': {0: '1'}}
print(dict['Age'][0])

to convert just do:

age = dict['Age'][0]
del(dict['Age'])
dict.update({"Age":age})

you will get the following dic as result:

{'DailyRate': {0: '903'}, 'DistanceFromHome': {0: '2'}, 'EnvironmentSatisfaction': {0: '1'}, 'HourlyRate': {0: '41'}, 'JobInvolvement': {0: '4'}, 'JobLevel': {0: '3'}, 'JobSatisfaction': {0: '3'}, 'MonthlyIncome': {0: '7880'}, 'MonthlyRate': {0: '2560'}, 'NumCompaniesWorked': {0: '0'}, 'PercentSalaryHike': {0: '18'}, 'RelationshipSatisfaction': {0: '4'}, 'StandardHours': {0: '80'}, 'TotalWorkingYears': {0: '9'}, 'TrainingTimesLastYear': {0: '3'}, 'YearsAtCompany': {0: '8'}, 'YearsInCurrentRole': {0: '7'}, 'YearsSinceLastPromotion': {0: '0'}, 'YearsWithCurrManager': {0: '7'}, 'MaritalStatus_': {0: '2'}, 'JobRole_': {0: '7'}, 'Gender_': {0: '1'}, 'EducationField_': {0: '1'}, 'Department_': {0: '2'}, 'BusinessTravel_': {0: '2'}, 'OverTime_': {0: '1'}, 'Over18_': {0: '1'}, 'Age': '39'}

Comments

0

Maybe try the following code:

keys = myDict.keys()
valuesWithZero = myDict.values()

valuesNoZero = []
for item in valuesWithZero:
    value_iterator = iter(item.values()) #to make dict_values obj iterable
    first_value = next(value_iterator) #obtaining first value
    valuesNoZero.append(first_value) #adding to new list
    

newDict = dict(zip(keys, valuesNoZero))  #combining keys arr and values arr
print(newDict)

# should output: {'Age': '39', 'DailyRate': '903', 'DistanceFromHome': '2', 'EnvironmentSatisfaction': '1', 'HourlyRate': '41', 'JobInvolvement': '4', 'JobLevel': '3', 'JobSatisfaction': '3', 'MonthlyIncome': '7880', 'MonthlyRate': '2560', 'NumCompaniesWorked': '0', 'PercentSalaryHike': '18', 'RelationshipSatisfaction': '4', 'StandardHours': '80', 'TotalWorkingYears': '9', 'TrainingTimesLastYear': '3', 'YearsAtCompany': '8', 'YearsInCurrentRole': '7', 'YearsSinceLastPromotion': '0', 'YearsWithCurrManager': '7', 'MaritalStatus_': '2', 'JobRole_': '7', 'Gender_': '1', 'EducationField_': '1', 'Department_': '2', 'BusinessTravel_': '2', 'OverTime_': '1', 'Over18_': '1'}

1 Comment

You should start learning how to write pythonic code
0

Same process as the accepted answer but uses some of Python's functional features.

import operator
zero = operator.itemgetter(0)

newdict = dict(zip(myDict,map(zero, myDict.values())))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.