Add Item to Dictionary

Add Item to Dictionary

Tags: Basics

Credit: CodingChallenge

Difficulty: Beginner

Type: Text

Category: Practice

Time Allowed: 5 s

Description:

Write a function that takes a dictionary, a key, and a value, and returns a new dictionary with the key-value pair added. This problem is designed to help you practice adding items to dictionaries. The function should accept three parameters and return the updated dictionary.

Constraints:

  • The first input will be a dictionary.
  • The second input will be a key.
  • The third input will be a value.
  • The function should return a new dictionary with the key-value pair added.
  • Do not modify the original dictionary.

Example:

  • Input: {"name": "John"}, "age", 30
  • Output: {"name": "John", "age": 30}

Test Case Input Expected Output
1 [{"name": "John"}, "age", 30] {"name": "John", "age": 30}
2 [{}, "key", "value"] {"key": "value"}
3 [{"a": 1, "b": 2}, "c", 3] {"a": 1, "b": 2, "c": 3}
4 [{"x": 10}, "x", 20] {"x": 20}
5 [{"list": [1,2]}, "new_list", [3,4]] {"list": [1, 2], "new_list": [3, 4]}

Please register or login to submit solutions to challenges.