Skip to content

What are the Comparisons Between Python Lists, Dictionaries, and JavaScript Arrays, and Objects?


How do Python lists and dictionaries compare to JavaScript arrays and objects?

1. Python List vs. JavaScript Array:

  • Python List: An ordered, mutable collection that can store items of different types. Lists are defined using square brackets.
  • JavaScript Array: An ordered, mutable collection that can also store items of various types. Arrays are defined using square brackets as well.
  • Python List:

    my_list = [1, 2, 3, "hello"]
    
  • JavaScript Array:

    const myArray = [1, 2, 3, "hello"];
    

Python Dictionary vs. JavaScript Object:

  • Python Dictionary: A mutable collection of key-value pairs where keys must be unique and immutable. Dictionaries are defined using curly braces.
  • JavaScript Object: A collection of key-value pairs (properties) where keys are strings (or Symbols) and can be dynamically added or modified. Objects are also defined using curly braces.
  • Python Dictionary:

    my_dict = {
        "name": "Alice",
        "age": 30
    }
    
  • JavaScript Object:

    const myObject = {
        name: "Alice",
        age: 30
    };
    

Summary

  • List (Python) is similar to Array (JavaScript): Both are ordered and mutable collections.
  • Dictionary (Python) is similar to Object (JavaScript): Both store key-value pairs and are mutable.