JS
PY

JavaScript to Python

10 lessons

Progress0%
1Variables and Constants2Functions3Arrays vs Lists4Objects vs Dictionaries5Classes and OOP6Modules and Imports7Array Methods vs Comprehensions8Error Handling9Async Programming10File I/O
All Mirror Courses
JS
PY
Objects vs Dictionaries
MirrorLesson 4 of 10
Lesson 4

Objects vs Dictionaries

Key-value data structures

Introduction

In this lesson, you'll learn about objects vs dictionaries in Python. Coming from JavaScript, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
JS
From JavaScript:

In JavaScript, you're familiar with key-value data structures.

PY
In Python:

Python has its own approach to key-value data structures, which we'll explore step by step.

The Python Way

Let's see how Python handles this concept. Here's a typical example:

PY
Python Example
person = {"name": "Alice", "age": 30}

# Access
print(person["name"])           # bracket only
print(person.get("age", 0))     # safe access with default

# Mutate
person["city"] = "Istanbul"
del person["age"]

# Iteration
for key, val in person.items():
    print(key, val)

# Unpacking
name = person["name"]
city = person.get("city", "Unknown")

# Merge (Python 3.9+)
updated = {**person, "age": 31}

Comparing to JavaScript

Here's how you might have written similar code in JavaScript:

JS
JavaScript (What you know)
const person = { name: "Alice", age: 30 };

// Access
console.log(person.name);       // dot notation
console.log(person["age"]);     // bracket notation

// Mutate
person.city = "Istanbul";
delete person.age;

// Iteration
for (const [key, val] of Object.entries(person)) {
  console.log(key, val);
}

// Destructuring
const { name, city = "Unknown" } = person;

// Spread
const updated = { ...person, age: 31 };
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

PY
In Python:

Python dicts require string keys in quotes

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

PY
In Python:

Use .get(key, default) for safe access

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

PY
In Python:

dict.items() ≈ Object.entries()

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

PY
In Python:

{**dict1, **dict2} ≈ {...obj1, ...obj2}

Step-by-Step Breakdown

1. Key Access

Python dicts only support bracket notation. Use .get() for safe access with a default value.

JS
JavaScript
person.name
person["name"]
PY
Python
person["name"]
person.get("name", "default")
Common Pitfall
person.name raises AttributeError in Python — always use brackets or .get().

2. Iteration

Use .items() to get key-value pairs, .keys() for keys, .values() for values.

JS
JavaScript
Object.entries(obj)  // [[k,v], ...]
Object.keys(obj)     // [k, ...]
Object.values(obj)   // [v, ...]
PY
Python
d.items()  # dict_items
d.keys()   # dict_keys
d.values() # dict_values

3. Merging Dictionaries

Python 3.9+ supports the | merge operator and ** unpacking in dict literals.

JS
JavaScript
const merged = { ...a, ...b };
PY
Python
merged = {**a, **b}   # Python 3.5+
merged = a | b        # Python 3.9+
Rule of Thumb
For production code targeting Python 3.8, stick with {**a, **b}.

Common Mistakes

When coming from JavaScript, developers often make these mistakes:

  • Python dicts require string keys in quotes
  • Use .get(key, default) for safe access
  • dict.items() ≈ Object.entries()
Common Pitfall
Don't assume Python works exactly like JavaScript. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • bracket access only — no dot notation
  • .get(key, default) for safe lookups
  • .items() / .keys() / .values() for iteration
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your JavaScript code in Python to practice these concepts.
PreviousNext