Python List vs array vs tuple vs dictionary

Sara 56 Published: 12/05/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!

difference between list, tuple, set and dictionary in python with example

I'm glad you asked!

In Python, list, tuple, set, and dictionary are four fundamental data structures that serve distinct purposes. Understanding the differences between them is crucial for writing efficient and effective code.

1. List

A list is a mutable, ordered collection of items that can be of any data type, including strings, integers, floats, and other lists. It's defined using square brackets []. Lists are denoted by indices starting from 0.

Example:

fruits = ['apple', 'banana', 'cherry']

print(fruits[0]) # Output: 'apple'

fruits.append('orange') # Adding a new element

print(fruits) # Output: ['apple', 'banana', 'cherry', 'orange']

2. Tuple

A tuple is an immutable, ordered collection of items that can be of any data type, including strings, integers, floats, and other tuples. It's defined using parentheses () or square brackets []. Tuples are denoted by indices starting from 0.

Example:

colors = ('red', 'green', 'blue')

print(colors[0]) # Output: 'red'

Attempting to modify a tuple will raise an error colors[0] = 'yellow' # This would raise a TypeError

3. Set

A set is an unordered, unindexed collection of unique items that can be of any data type. It's defined using the {} operator.

Example:

numbers = {1, 2, 2, 3, 4}

print(len(numbers)) # Output: 4 (unique values)

print(2 in numbers) # Output: True

numbers.add(5) # Adding a new element

print(numbers) # Output: {1, 2, 3, 4, 5}

4. Dictionary

A dictionary is an unordered collection of key-value pairs where each key is unique and maps to a specific value. It's defined using the {} operator with keys separated by commas.

Example:

person = {'name': 'John', 'age': 30, 'city': 'New York'}

print(person['name']) # Output: 'John'

person['country'] = 'USA' # Adding a new key-value pair

print(person) # Output: {'name': 'John', 'age': 30, 'city': 'New York', 'country': 'USA'}

In summary:

list is a mutable collection of items that can be of any data type. tuple is an immutable collection of items that can be of any data type. set is an unordered collection of unique items that can be of any data type. dictionary is an unordered collection of key-value pairs where each key is unique and maps to a specific value.

Understanding the differences between these data structures is essential for writing effective Python code.