dict.update()

Python · Dictionaries

Theory

.update(other) merges key-value pairs into a dictionary, in place. For each pair in other: if the key already exists its value is overwritten, and if it is new it is added at the end. employee.update({'salary': 70000, 'bonus': 5000}) changes salary to 70000 and adds bonus, giving {'name': 'Asha', 'salary': 70000, 'bonus': 5000}. It returns None, so do not write employee = employee.update(...).

update() accepts another dict, an iterable of (key, value) pairs, or keyword arguments: employee.update(city='Pune'). When several sources contain the same key, the last one wins. If you want a new merged dict and must keep the original unchanged, use the merge operator new = a | b (Python 3.9+) or {**a, **b}; a |= b is the in-place version, like update(). update() is a shallow merge: nested dicts are replaced as a whole, not merged recursively.

Why we use it

Applying changes to a record, overlaying user settings on defaults, or combining data from two sources are everyday tasks. update() does all of it in one call instead of assigning keys one by one.

When to use it

Use update() to change or add several keys in an existing dict. Use a | b or {**a, **b} when you need a new dict and must keep the originals. For nested configs that need deep merging, write a small recursive function.

How it works

  1. Call employee.update({'salary': 70000, 'bonus': 5000})
  2. Take the next pair from the argument
  3. Is the key already in employee? Yes: Overwrite its value in place. No: Add the new pair at the end.
  4. Repeat for every pair
  5. Return None; employee now holds the merged data

Syntax

dict.update(other_dict)

Example

employee = {'name': 'Asha', 'salary': 65000}
employee.update({'salary': 70000, 'bonus': 5000})
print(employee)

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic