A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Write a python program to rename a key city to a location in the following dictionary.
Q:

Write a python program to rename a key city to a location in the following dictionary.

0

Rename key of a dictionary

Write a program to rename a key city to a location in the following dictionary.

Given:

sample_dict = {
  "name": "Kelly",
  "age":25,
  "salary": 8000,
  "city": "New york"
}

Expected output:

{'name': 'Kelly', 'age': 25, 'salary': 8000, 'location': 'New york'}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Hint:

  • Remove the city from a given dictionary
  • Add a new key (location) into a dictionary with the same value

Solution:

sample_dict = {
    "name": "Kelly",
    "age": 25,
    "salary": 8000,
    "city": "New york"
}

sample_dict['location'] = sample_dict.pop('city')
print(sample_dict)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now