SoFunction
Updated on 2025-03-02

In-depth discussion of common pitfalls and avoidance methods for Python composite data

In Python, composite data (such as lists, tuples, collections, and dictionaries) are very common data types that can organize and manipulate data in a structured way. However, due to its flexibility and characteristics, some pitfalls and problems are often prone to occur when using composite data. This guide will dig into the common pitfalls of Python composite data and provide some practical suggestions and tips to avoid these issues to help better utilize Python composite data.

Lists

1. Modify variable objects

Lists are variable data types, so be extra careful when operating on mutable objects in a list (such as lists, dictionaries, etc.). When modifying mutable objects in a list, it is easy to affect the original list.

# Modifying mutable objects will affect the original listoriginal_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
modified_list = original_list
modified_list[0][0] = 100
print(original_list)  # Output: [[100, 2, 3], [4, 5, 6], [7, 8, 9]]

2. Shallow copy and deep copy

When copying a list, you should understand the difference between a shallow copy and a deep copy. A shallow copy will only copy the top-level elements of the list, while a deep copy will recursively copy all nested objects.

import copy

original_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# Shallow copyshallow_copy = (original_list)
shallow_copy[0][0] = 100
print(original_list)  # Output: [[100, 2, 3], [4, 5, 6], [7, 8, 9]]
# Deep copydeep_copy = (original_list)
deep_copy[0][0] = 1000
print(original_list)  # Output: [[100, 2, 3], [4, 5, 6], [7, 8, 9]]

Tuples

Tuples are immutable data types, so they cannot be modified. However, it should be noted that if the tuple contains a mutable object, the content of the mutable object can be modified.

# contains mutable objects in tuplestuple_with_list = ([1, 2, 3], [4, 5, 6])
tuple_with_list[0][0] = 100
print(tuple_with_list)  # Output: ([100, 2, 3], [4, 5, 6])

Sets

A set is an unordered and non-repetitive data type, which is often used for deduplication and set operations. However, due to its non-indexable nature, unexpected results may sometimes result.

# Collections do not support indexingmy_set = {1, 2, 3}
print(my_set[0])  # Report an error: 'set' object is not subscriptable

Dictionaries

1. Key value uniqueness

The keys of the dictionary must be unique, and if you try to add a new key-value pair with the same key, the original key-value pair will be overwritten.

my_dict = {'a': 1, 'b': 2}
my_dict['a'] = 100
print(my_dict)  # Output: {'a': 100, 'b': 2}

2. Key type

The keys of a dictionary can be immutable data types, such as strings, integers, tuples, etc., but they cannot be variable data types, such as lists, sets, dictionaries, etc.

# The key of the dictionary cannot be a listmy_dict = {[1, 2]: 'value'}  # Report an error: unhashable type: 'list'

Practical application scenarios

Composite data has a wide range of applications in Python, and they can be seen from data analysis to software development. Through some practical application scenarios, we can further understand how to avoid pitfalls in practice and use composite data correctly.

1. Data analysis and cleaning

In data analysis, it is often necessary to process composite data from various data sources, such as JSON format data, nested dictionaries and lists.

Here is a simple example that demonstrates how to read data from a JSON file and clean and process it.

import json

# Read JSON filewith open('', 'r') as f:
    data = (f)

# Extract data and cleancleaned_data = []
for item in data:
    if 'name' in item and 'age' in item:
        cleaned_data.append({'name': item['name'], 'age': item['age']})

# Print the cleaned dataprint(cleaned_data)

In this example, a JSON file is first read, then the data is traversed and cleaned, leaving only the data containing the 'name' and 'age' fields.

2. Web crawler and data extraction

In web crawler development, it is often necessary to process compound data in HTML pages, such as extracting table data, links and text content.

See an example that demonstrates how to extract table data from a webpage using the BeautifulSoup library.

from bs4 import BeautifulSoup
import requests

​​​​​​​# Send HTTP request to get web page contenturl = ''
response = (url)
html_content = 

# Use BeautifulSoup to parse web contentsoup = BeautifulSoup(html_content, '')

# Extract table datatable = ('table')
if table:
    rows = table.find_all('tr')
    data = []
    for row in rows:
        cells = row.find_all('td')
        if cells:
            row_data = [() for cell in cells]
            (row_data)

# Print extracted table dataprint(data)

In this example, the requests library is used to send an HTTP request to get the content of the web page, and then the HTML content is parsed using the BeautifulSoup library and the tabular data is extracted.

3. Software development and data structure design

In software development, the rational design and use of composite data structures can improve the readability, maintainability and performance of the code.

See an example that demonstrates how to design a simple data structure to represent student information.

class Student:
    def __init__(self, name, age, courses):
         = name
         = age
         = courses

​​​​​​​    def __repr__(self):
        return f"Student(name={}, age={}, courses={})"

# Create a student objectstudent1 = Student('Alice', 20, ['Math', 'Physics', 'Chemistry'])
student2 = Student('Bob', 22, ['History', 'Literature', 'Geography'])

# Print student informationprint(student1)
print(student2)

In this example, a Student class is defined to represent student information, including name, age, and courses taken. Then, two student objects are created and their information is printed.

4. Database operations and ORM framework

When operating databases and using ORM (Object Relational Mapping) frameworks, it is often necessary to process composite data, such as query result sets, model objects and associated data.

Here is a simple example that demonstrates how to use the SQLAlchemy ORM framework to define models and query data.

from sqlalchemy import create_engine, Column, Integer, String
from  import declarative_base
from  import sessionmaker

# Create database engine and sessionengine = create_engine('sqlite:///:memory:')
Base = declarative_base()
Session = sessionmaker(bind=engine)
session = Session()

# Define the model classclass Product(Base):
    __tablename__ = 'products'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    price = Column(Integer)

# Create a data table.create_all(engine)

# Create a product object and insert dataproduct1 = Product(name='Product 1', price=100)
product2 = Product(name='Product 2', price=200)
(product1)
(product2)
()

# Query dataproducts = (Product).all()

# Print query resultsfor product in products:
    print(, )

In this example, the SQLAlchemy ORM framework is used to define a simple product model, then two product objects are created and data is inserted, and all product data is finally queryed and printed out.

Summarize

This article describes common pitfalls and problems when using Python composite data, and provides some practical suggestions and tips to avoid these problems. By gaining insight into the features of lists, tuples, collections, and dictionaries, and how to use them correctly, you can better utilize Python's composite data and write more robust and efficient code.

This is the article about in-depth discussion of common pitfalls and avoidance methods for Python composite data. For more related Python composite data content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!