What are the 4 data structures in Python?

Lorin 69 Published: 09/09/2024

What are the 4 data structures in Python?

I'm happy to help! However, please note that I'll respond in English as per your request.

Now, let's dive into the world of Python data structures!

Python offers a wide range of built-in and external libraries to work with various data structures. The four fundamental data structures in Python are:

Lists: Lists are one of the most commonly used data structures in Python. They're implemented as arrays, which means they store elements in contiguous blocks of memory. You can think of lists as arrays that grow or shrink dynamically. In a list, elements are ordered and allow for duplicate values.

Here's an example:

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

print(my_list) # Output: [1, 2, 3, 'Hello', True]

Lists are mutable, meaning they can be modified after creation. You can use indexing (e.g., my_list[0]) and slicing (e.g., my_list[:2]) to access or manipulate list elements.

Tuples: Tuples are similar to lists but are immutable, meaning their contents cannot be changed once created. They're often used when you need a fixed, unordered collection of values.

Here's an example:

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

print(my_tuple) # Output: (1, 2, 3, 'Hello', True)

Tuples are also ordered and allow for duplicate values. They're useful when you need a lightweight alternative to lists.

Sets: Sets are unordered collections of unique elements. They don't allow duplicate values or preserve the order in which elements were added. Sets are mutable and can be used to keep track of unique elements.

Here's an example:

my_set = {1, 2, 2, 3}

print(my_set) # Output: {1, 2, 3} (no duplicates)

Sets provide a fast way to check membership (i.e., whether a specific element is present) and remove duplicate values from your data.

Dictionaries: Dictionaries are mutable data structures that store elements as key-value pairs. They're implemented as hash tables, which means they allow for efficient lookup, insertion, and deletion operations based on keys. Dictionaries can contain duplicate values and preserve the order in which items were added.

Here's an example:

my_dict = {"Name": "John", "Age": 30}

print(my_dict) # Output: {'Name': 'John', 'Age': 30}

Dictionaries are essential for storing structured data, such as JSON objects or key-value pairs.

These four fundamental data structures are the foundation of Python programming. They're used extensively in various applications, from web development to scientific computing and data analysis. Understanding how to work with lists, tuples, sets, and dictionaries will give you a solid grasp of Python programming.

Python data structures w3schools

I'm happy to help! Here's the information on Python data structures from W3Schools:

Python Lists

A list is a mutable sequence of values, which means you can add or remove items after it's created.

my_list = [1, 2, 3]

print(my_list) # Output: [1, 2, 3]

Add an item

my_list.append(4)

print(my_list) # Output: [1, 2, 3, 4]

Remove the first occurrence of "3"

my_list.remove(3)

print(my_list) # Output: [1, 2, 4]

You can also use list methods like sort(), reverse(), and index().

Python Tuples

A tuple is an immutable sequence of values, which means you can't add or remove items after it's created.

my_tuple = (1, 2, 3)

print(my_tuple) # Output: (1, 2, 3)

Try to append an item

my_tuple.append(4) # Raises a TypeError!

print(my_tuple) # Still the same output: (1, 2, 3)

Python Dictionaries

A dictionary is an unordered collection of key-value pairs.

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

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

Add a new item

my_dict["city"] = "New York"

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

Remove an item

del my_dict["age"]

print(my_dict) # Output: {'name': 'John', 'city': 'New York'}

Python Sets

A set is an unordered collection of unique values.

my_set = {1, 2, 3}

print(my_set) # Output: {1, 2, 3}

Add a new item

my_set.add(4)

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

Remove an item

my_set.discard(2)

print(my_set) # Output: {1, 3, 4}

These are the basics of Python data structures. You can use them to store and manipulate data in your programs!

Please let me know if you need anything else!