Javascript equivalent to Python Dictionary
Last Updated :
04 Dec, 2024
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);
Similar Reads
How to read Dictionary from File in Python? In Python, reading a dictionary from a file involves retrieving stored data and converting it back into a dictionary format. Depending on how the dictionary was savedâwhether as text, JSON, or binary-different methods can be used to read and reconstruct the dictionary for further use in your program
3 min read
How to Add Duplicate Keys in Dictionary - Python In Python, dictionaries are used to store key-value pairs. However, dictionaries do not support duplicate keys. In this article, we will explore several techniques to store multiple values for a single dictionary key.Understanding Dictionary Key ConstraintsIn Python, dictionary keys must be unique.
3 min read
Convert nested Python dictionary to object Let us see how to convert a given nested dictionary into an object Method 1 : Using the json module. We can solve this particular problem by importing the json module and use a custom object hook in the json.loads() method. python3 # importing the module import json # declaringa a class class obj: #
2 min read
Python Dictionary Methods Python dictionary methods is collection of Python functions that operates on Dictionary.Python Dictionary is like a map that is used to store data in the form of a key: value pair. Python provides various built-in functions to deal with dictionaries. In this article, we will see a list of all the fu
5 min read
Dictionaries in Python Python dictionary is a data structure that stores the value in key: value pairs. Values in a dictionary can be of any data type and can be duplicated, whereas keys can't be repeated and must be immutable. Example: Here, The data is stored in key:value pairs in dictionaries, which makes it easier to
5 min read
Python | Set 4 (Dictionary, Keywords in Python) In the previous two articles (Set 2 and Set 3), we discussed the basics of python. In this article, we will learn more about python and feel the power of python. Dictionary in Python In python, the dictionary is similar to hash or maps in other languages. It consists of key-value pairs. The value c
5 min read