-1

Given a dictionary like this:

{'Fruit': 'PAR',
'Brand': 'best',
'date': 'imorgon',
'type': 'true',
'class': 'Good',
'time': '2018-10-25',
'number': 234}

How can I replace a value associated to one key, with the value of another key? For example, I would like to delete and replace in date value, the value of time:

{'Fruit': 'PAR',
'Brand': 'best',
'date': '2018-10-25',      <----- HERE
'type': 'true',
'class': 'Good',
                           <----- This one is removed and replaced into the data key
'number': 234}

I tried to:

{value : key for key,value in a.items()}

However its just switching the keys for values.

anon
  • 836
  • 2
  • 9
  • 25

1 Answers1

3

You could use the normal assignment and pop:

data = {'Fruit': 'PAR',
'Brand': 'best',
'date': 'imorgon',
'type': 'true',
'class': 'Good',
'time': '2018-10-25',
'number': 234}

data['date'] = data.pop('time')

print(data)

Output

{'date': '2018-10-25', 'type': 'true', 'number': 234, 'Fruit': 'PAR', 'class': 'Good', 'Brand': 'best'}
Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76