Skip to content

[Edit] Python Dictionaries: .update() #6680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 13, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[Edit] Python Dictionaries: .update()
  • Loading branch information
Sriparno08 committed May 3, 2025
commit 3af00ce3947beb500bb69d4ffe7672ee44055fe7
89 changes: 79 additions & 10 deletions content/python/concepts/dictionaries/terms/update/update.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,104 @@
---
Title: '.update()'
Description: 'Adds the entries in a specified dictionary, or iterable of key-value pairs, to a dictionary.'
Description: 'Adds the entries from a dictionary or an iterable of key-value pairs to another dictionary.'
Subjects:
- 'Computer Science'
- 'Data Science'
Tags:
- 'Dictionaries'
- 'Functions'
- 'Methods'
- 'Values'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

The `.update()` method returns new Python dictionary with entries from another dictionary, or some other iterable, added to it.
In Python, the **`.update()`** method adds the entries from a dictionary or an [iterable](https://www.codecademy.com/resources/docs/python/iterators) of key-value pairs to another dictionary. This method is helpful in various scenarios, such as updating user profiles with new data, merging configuration dictionaries, or synchronizing values across multiple data sources.

## Syntax

```pseudo
dictionary.update(entries)
dict.update([other])
```

Where `entries` is another dictionary or an iterable of key-value pairs. Pairs in `dictionary` are replaced by any pair in `entries` with a duplicate key.
**Parameters:**

## Codebyte Example
- `other`: This can be a dictionary or an iterable of key-value pairs (like a list of [tuples](https://www.codecademy.com/resources/docs/python/tuples)). If there is a duplicate key in `dict` and `other`, the key-value pair in `dict` is replaced with the key-value pair in `other`.

The following example creates two dictionaries, then adds the entries from one to the other.
**Return value:**

The `.update()` method updates the dictionary in place and returns `None`.

## Example 1: Basic Usage of `.update()`

This example uses the `.update()` method to add the entries from one dictionary to another:

```py
# Create two dictionaries
person = {'name': 'Alice', 'age': 25}
new_info = {'city': 'New York', 'age': 26}

# Add the entries from 'info' to 'person'
person.update(new_info)

# Print the modified 'person' dictionary
print(person)
```

Here is the output:

```shell
{'name': 'Alice', 'age': 26, 'city': 'New York'}
```

## Example 2: Using `.update()` with a List of Tuples

This example uses the `.update()` method to add the entries from an iterable of key-value pairs (list of tuples) to a dictionary:

```py
# Create a dictionary and a tuple
settings = {'theme': 'light', 'notifications': True}
new_settings = [('theme', 'dark'), ('volume', 'medium')]

# Add the entries from 'new_settings' to 'settings'
settings.update(new_settings)

# Print the modified 'settings' dictionary
print(settings)
```

Here is the output:

```shell
{'theme': 'dark', 'notifications': True, 'volume': 'medium'}
```

## Codebyte Example: Using `.update()` with Keyword Arguments

This codebyte example uses the `.update()` method with keyword arguments to add entries to a dictionary:

```codebyte/python
d1 = {1:'one',2:'two', 3:'three'}
d2 = {4:'four', 5:'five', 6:'six'}
d1.update(d2)
print(d1)
# Create a dictionary
config = {'debug': False}

# Add entries to 'config'
config.update(debug=True, log_level='INFO')

# Print the modified 'config' dictionary
print(config)
```

## Frequently Asked Questions

### 1. What if the argument to `.update()` is `None` or not iterable?

Passing `None` or a non-iterable object to `.update()` will raise a [`TypeError`](https://www.codecademy.com/resources/docs/python/errors). You must provide a dictionary or an iterable of key-value pairs.

### 2. Is `.update()` thread-safe?

No, `.update()` is not [thread](https://www.codecademy.com/resources/docs/python/threading)-safe. If you're working with shared dictionaries in a multi-threaded environment, use locks to avoid race conditions.

### 3. Can I chain `.update()` calls?

Since `.update()` returns `None`, it cannot be chained directly. If you want to apply multiple updates, call `.update()` separately for each.