Python List vs array vs tuple vs dictionary

Natasha 63 Published: 06/30/2024

Python List vs array vs tuple vs dictionary

Here's a detailed explanation of the four fundamental data structures in Python: lists, arrays, tuples, and dictionaries:

In Python, a list is a mutable sequence of items that can be of any data type, including strings, integers, floats, and other lists. Lists are denoted by square brackets [] and are created using the comma-separated values inside these brackets. For example: my_list = [1, 2, 3, 'hello', 4.5].

Lists have the following properties:

They are mutable, meaning their contents can be modified after creation. They support indexing (accessing elements by their position) and slicing (extracting a subset of elements). They can contain duplicate values. They are dynamic in size, growing or shrinking as elements are added or removed.

Arrays

Python does not have built-in arrays like some other languages. However, you can use the array module to create arrays from the NumPy library. These arrays are similar to lists but are more memory-efficient and optimized for numerical computations.

NumPy arrays have the following properties:

They are mutable. They support indexing and slicing. They can contain numeric data only (no strings or other types). They are designed for fast numerical computation.

A tuple is an immutable sequence of items, similar to a list. Tuples are denoted by parentheses () and are created using the comma-separated values inside these brackets. For example: my_tuple = (1, 2, 3, 'hello', 4.5).

Tuples have the following properties:

They are immutable, meaning their contents cannot be modified after creation. They support indexing and slicing. They can contain duplicate values. They are static in size, not growing or shrinking dynamically.

A dictionary is an unordered collection of key-value pairs. Dictionaries are denoted by curly braces {} and are created using the key: value syntax inside these brackets. For example: my_dict = {'name': 'John', 'age': 30}.

Dictionaries have the following properties:

They are mutable, meaning their contents can be modified after creation. They support key-based indexing (accessing values by their corresponding keys). Keys must be unique; dictionaries cannot contain duplicate keys. Values can be of any data type.

In summary:

Lists are dynamic, mutable sequences suitable for general-purpose use. Arrays (NumPy) are memory-efficient arrays optimized for numerical computations. Tuples are immutable sequences useful when you need to store a fixed set of values. Dictionaries are unordered collections of key-value pairs ideal for storing and retrieving data with associated labels.

I hope this helps clarify the differences between these fundamental data structures in Python!

Python list vs array vs tuple vs dictionary vs set

The infamous "Python collection types" conundrum! Let's dive into the world of lists, arrays, tuples, dictionaries, and sets in Python.

List

A list is a mutable, ordered collection of items that can be of any data type, including strings, integers, floats, and other lists. Lists are denoted by square brackets [] and elements are separated by commas. Lists are dynamic, meaning you can add or remove elements at runtime. For example:

my_list = [1, 2, 3, "hello", True]

print(my_list[0]) # Output: 1

my_list.append("world")

print(my_list) # Output: [1, 2, 3, "hello", True, "world"]

Array

In Python, there is no built-in array type like in languages like C or Java. Instead, you can use lists to create arrays-like data structures. While not exactly the same as a native array, lists behave similarly when used with integer indices and slicing.

Tuple

A tuple is an immutable, ordered collection of items that can be of any data type. Tuples are denoted by parentheses () and elements are separated by commas. Like lists, tuples support indexing and slicing, but once created, they cannot be modified. For example:

my_tuple = (1, 2, 3, "hello", True)

print(my_tuple[0]) # Output: 1

try:

my_tuple.append("world") # Error!

except AttributeError:

print("Tuples are immutable!")

Dictionary

A dictionary is an unordered collection of key-value pairs. Dictionaries are denoted by curly braces {} and key-value pairs are separated by commas. Each key must be unique, but values can be repeated. For example:

my_dict = {"name": "John", "age": 30}

print(my_dict["name"]) # Output: John

my_dict["location"] = "New York"

print(my_dict) # Output: {'name': 'John', 'age': 30, 'location': 'New York'}

Set

A set is an unordered collection of unique elements. Sets are denoted by curly braces {} and elements are separated by commas. Sets automatically eliminate duplicates and do not preserve the order in which elements were added. For example:

my_set = {"apple", "banana", "apple"}

print(len(my_set)) # Output: 2 (only unique elements)

print("apple" in my_set) # Output: True

In summary:

Lists are mutable, ordered collections that can be modified. Tuples are immutable, ordered collections that cannot be modified. Dictionaries are unordered collections of key-value pairs with unique keys. Sets are unordered collections of unique elements without duplicates.

While Python's collection types share some similarities with those in other languages, they have distinct characteristics that make them powerful and versatile. Now go forth and harness the power of Python's collection types!