Open In App

Javascript equivalent to Python Dictionary

Last Updated : 04 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

A dictionary is a powerful data structure allowing us to store and access data using keys quickly. In Python, we use a dictionary to store key-value pairs. In JavaScript, the closest equivalent to a Python dictionary is the Object or Map. The choice between a JavaScript Object or Map depends on the nature of your keys and the performance characteristics needed for your application.

Declaration

Python Dictionary

In Python, a dictionary is declared using curly braces '{}' with key-value pairs separated by colons ':'. Keys must be immutable (e.g., strings, numbers, tuples), while values can be any data type.

Python
# Declaring a Python dictionary
my_dict = {
    "name": "John",        # Key: "name", Value: "John"
    "age": 30,             # Key: "age", Value: 30
    "is_active": True      # Key: "is_active", Value: True
}

JavaScript Object

In JavaScript, an object is declared using curly braces '{}' with key-value pairs. The keys are automatically converted to strings and the values can be any data type.

JavaScript
// Declaring a JavaScript object
let myObj = {
    name: "John",         // Key: "name", Value: "John"
    age: 30,              // Key: "age", Value: 30
    isActive: true        // Key: "isActive", Value: true
};

JavaScript Map

In JavaScript, a Map is created using the new Map() constructor. You can then add key-value pairs using the .set() method. Map keys can be of any type, including objects, functions, and primitive types.

JavaScript
// Declaring a JavaScript Map
let myMap = new Map();
myMap.set("name", "John");          // Key: "name", Value: "John"
myMap.set("age", 30);               // Key: "age", Value: 30
myMap.set("isActive", true);        // Key: "isActive", Value: true

Order of Keys

Python dictionary

In Python 3.7 and later, dictionaries preserve the insertion order of keys.

Python
my_dict = {"a": 1, "b": 2, "c": 3}
for key in my_dict:
    print(key)  

JavaScript Object

For non-numeric keys, objects typically preserve insertion order however numeric keys are ordered by their numeric value when iterating.

JavaScript
let obj = { 1: 1, 3: 2, 2: 3 };
for (let key in obj) {
    console.log(key);  
}

JavaScript Map

Maps guarantee that keys are iterated in the order of insertion whether the keys are numbers, strings, or any other data type.

JavaScript
let map = new Map();
map.set("a", 1);
map.set("b", 2);
map.set("c", 3);
for (let [key, value] of map) {
    console.log(key);  }

Methods and Functionality

Python Dictionary

Python dictionaries come with several built-in methods like keys(), values(), items(), get(), pop(), clear(), etc., which allow easy manipulation of the key-value pairs.

Python
my_dict = {"a": 1, "b": 2, "c": 3}
print(my_dict.get("a"))  
print(my_dict.keys()) 

JavaScript Object

JavaScript objects have no built-in methods for manipulating key-value pairs directly. Instead, you can use methods like Object.keys(), Object.values(), and Object.entries() for working with objects, and the hasOwnProperty() method to check if an object has a specific key.

JavaScript
let obj = { "a": 1, "b": 2, "c": 3 };
console.log(Object.keys(obj));  

JavaScript Map

Maps provide a more rich API with methods like set(), get(), delete(), has(), size(), and clear(). Maps also have better iteration support and do not suffer from the issues of inheritance from the Object.prototype.

JavaScript
let map = new Map();
map.set("a", 1);
map.set("b", 2);
console.log(map.has("a"));  
console.log(map.size);  

Practice Tags :

Similar Reads