Comparison of Arrays, Lists, Dicts, and Sets Across Python, C++, and JavaScript
✅ 1. Arrays vs Dicts in Each Language
Concept | Python | C++ | JavaScript |
---|---|---|---|
Array / List | list = [10, 20, 30] |
int arr[3] = {10, 20, 30}; or vector<int> arr = {10, 20, 30};
|
let arr = [10, 20, 30]; |
Dictionary / Map (Key-Value) | d = {"a": 1, "b": 2} |
map<string, int> d = {{"a", 1}, {"b", 2}}; |
let d = {a: 1, b: 2}; or let d = new Map([["a", 1], ["b", 2]]);
|
Set (Unique elements, unordered) | s = {1, 2, 3} |
set<int> s = {1, 2, 3}; |
let s = new Set([1, 2, 3]); |
✅ 2. Why "Array" is called "List" in Python & JavaScript
- In C++,
array
is a low-level fixed-size container. Example:
int arr[3] = {1, 2, 3}; // Size is 3, cannot change
- In Python and JavaScript, arrays are dynamic and resizable, so they are closer to C++
vector
:
arr = [1, 2, 3]
arr.append(4) # can grow
let arr = [1, 2, 3];
arr.push(4); // can grow
👉 That’s why Python calls it list
, not array
, because it’s flexible.
👉 JS still calls it Array
, but it behaves like a Python list.
✅ 3. Why C++ Arrays are Declared with {}
not []
-
Square brackets
[]
→ specify size/index.
int arr[3]; // array of size 3
-
Curly braces
{}
→ specify values for initialization.
int arr[3] = {10, 20, 30}; // initialize
So:
-
[]
→ size -
{}
→ values
✅ 4. C++ 3D Array Example
A 3D array = array of arrays of arrays.
Example: a cube 2 × 3 × 4
#include <iostream>
using namespace std;
int main() {
int arr[2][3][4] = {
{ {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} },
{ {13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24} }
};
cout << arr[1][2][3]; // Output: 24
}
👉 Think of it as a box with 2 layers
, each having 3 rows
, each row having 4 columns
.
✅ 5. Sets in Each Language
- Python
s = {1, 2, 3, 3}
print(s) # {1, 2, 3} (removes duplicates)
- C++
#include <set>
set<int> s = {1, 2, 3, 3};
for (int x : s) cout << x << " "; // 1 2 3
- JavaScript
let s = new Set([1, 2, 3, 3]);
console.log(s); // Set(3) {1, 2, 3}
👉 Difference with {}
in Python
-
{}
in Python by default = dict (empty dictionary). -
set()
must be used for empty set.
a = {} # dictionary
b = set() # empty set
👉 In C++ {}
is not a set—it’s array initializer.
✅ 6. Quick Example Comparison
Array / List
- Python:
arr = [10, 20, 30]
- C++:
int arr[3] = {10, 20, 30};
- JS:
let arr = [10, 20, 30];
Dictionary / Map
- Python:
{"x": 1, "y": 2}
- C++:
map<string, int> d = {{"x",1},{"y",2}};
- JS:
{x:1, y:2}
ornew Map([["x",1],["y",2]])
Set
- Python:
{1, 2, 3}
- C++:
set<int> s = {1, 2, 3};
- JS:
new Set([1, 2, 3])
✅ So summary:
- C++ arrays = low-level, fixed size.
- Python list / JS Array = dynamic array (like C++ vector).
- Dict/Map = key-value store (different thing).
- Set = unique elements in all three.
Top comments (0)