Data Structure Types in Python: Lists, Tuples, Dictionaries, and Sets

In Python, data types are divided into basic data types and data structure types. The basic data types in Python include four types: integers, floating-point numbers, booleans, and strings, as detailed in Basic Data Types in Python.

Basic data types alone are not sufficient; sometimes, it is necessary to handle data in groups, which requires “container data types”, i.e., data structures.

The data structure types in Python include lists, tuples, dictionaries, and sets. Among these, lists and tuples belong to the sequence type in Python, dictionaries belong to the mapping type, and sets belong to the set type.

Characteristics Distinction of Python Data Structure Types

Type Representation Ordered Mutable Unique
List [] Yes Yes No
Tuple () Yes No No
Dictionary {} No Yes Keys are unique, values can be duplicated
Set {} No Yes Yes

1. List

Elements in a list can be changed, making it a mutable sequence, with elements separated by commas.

Representation: A list is represented using square brackets.

l=["Zhang San","Male",15]

Main Characteristics: Ordered, elements can be modified.

Suitable Scenarios: Storing mutable data, such as age, height, etc.

Usage:

Data Structure Types in Python: Lists, Tuples, Dictionaries, and Sets

2. Tuple

A tuple is an immutable sequence, meaning the elements within a tuple cannot be modified, with elements separated by commas.

Representation: A tuple is represented using parentheses.

t=("20251122","Li Si","Male")

Main Characteristics: Ordered, elements cannot be modified.

Suitable Scenarios: Storing fixed data, such as student ID numbers or ID cards.

Usage:

Data Structure Types in Python: Lists, Tuples, Dictionaries, and Sets

The elements in a tuple cannot be added, deleted, or modified, but if an element in the tuple is a mutable data type, then the value of that element can be modified, such as if the element is a list.

3. Dictionary

A dictionary expresses a mapping relationship using “key-value pairs”, with a colon separating the keys and values. Keys in a dictionary cannot be duplicated, while values can be.

Representation: A dictionary is represented using curly braces.

d={"Zhang San":15,"Li Si":14,"Wang Wu":15}

Main Characteristics: Unordered, stores key-value pairs, keys are unique.

Suitable Scenarios: Storing structured data, such as product information.

Usage:

Data Structure Types in Python: Lists, Tuples, Dictionaries, and Sets

4. Set

A set is used to store unordered elements that are unique.

Representation: A set is represented using curly braces.

s={"Beijing","Nanjing","Shanghai"}

Main Characteristics: Unordered, elements are unique.

Suitable Scenarios: Storing unique data, automatically removing duplicate elements.

Usage:

Data Structure Types in Python: Lists, Tuples, Dictionaries, and Sets

Summary:

Lists belong to the sequence type in Python, which is a collection of elements stored in a specific order, including lists and tuples. The elements in a list can be modified, making it a mutable sequence, while the elements in a tuple cannot be modified, making it an immutable sequence.

Mapping represents correspondence, commonly using key-value pairs for storage, where keys cannot be duplicated, but values can be. A typical mapping type is a dictionary.

A set is used to store unordered elements that are unique, which looks similar to a dictionary.

If you find this useful, remember to like + bookmark, and feel free to share it with friends who need it!

Previous Highlights:

Input and Output Functions in Python

Three Rules for Variable Naming in Python

Is Python a Compiled or Interpreted Language?

Common Operators in Python: Arithmetic, Logical, and Comparison Operators

Comments in Python

Basic Data Types in Python

Two Ways to Run Python Programs: Interactive and File-Based

The Origin of Python – How Can It Be Improved?

Leave a Comment