DEV Community

Muhammad Atif Iqbal
Muhammad Atif Iqbal

Posted on

Arrays, Lists, Dicts, and Sets Across Python, C++, and JavaScript

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 
Enter fullscreen mode Exit fullscreen mode
  • 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 
Enter fullscreen mode Exit fullscreen mode
 let arr = [1, 2, 3]; arr.push(4); // can grow 
Enter fullscreen mode Exit fullscreen mode

👉 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 
Enter fullscreen mode Exit fullscreen mode
  • Curly braces {} → specify values for initialization.
 int arr[3] = {10, 20, 30}; // initialize 
Enter fullscreen mode Exit fullscreen mode

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 } 
Enter fullscreen mode Exit fullscreen mode

👉 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) 
Enter fullscreen mode Exit fullscreen mode
  • C++
 #include <set>  set<int> s = {1, 2, 3, 3}; for (int x : s) cout << x << " "; // 1 2 3 
Enter fullscreen mode Exit fullscreen mode
  • JavaScript
 let s = new Set([1, 2, 3, 3]); console.log(s); // Set(3) {1, 2, 3} 
Enter fullscreen mode Exit fullscreen mode

👉 Difference with {} in Python

  • {} in Python by default = dict (empty dictionary).
  • set() must be used for empty set.
a = {} # dictionary b = set() # empty set 
Enter fullscreen mode Exit fullscreen mode

👉 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} or new 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)