Skip to content

3.4.3.7 Advanced Types

Python's advanced data types include four core container types: lists, tuples, dictionaries, and sets. These provide powerful capabilities for organizing and processing data in program development. Each type has its own unique characteristics and use cases, enabling it to meet a variety of programming needs.

image

Advanced Types Note Features
List Lists are one of the most commonly used data structures in Python and are denoted by square brackets [ ]. They are ordered, mutable collections of elements that can store data of any type. 1. Elements are stored in the order they are inserted, and each element has a corresponding index.
2. Supports dynamic insertion, deletion, and modification operations.
3. Can contain mixed data types (numbers, strings, and even other lists).
4. Supports slicing operations, making it easy to extract subsets.
Tuple Tuples are created using parentheses ( ) and are an ordered but immutable data structure. Once created, their contents cannot be modified. 1. Elements are arranged in a specific order and can be accessed by index.
2. Immutability ensures data integrity and prevents accidental modifications.
3. They are more memory-efficient than lists and offer faster access speeds.
4. They support tuple unpacking, which facilitates multi-variable assignment.
Dictionary A dictionary is created using curly braces { } and stores data in the form of key-value pairs. It provides fast key-based lookup capabilities. 1. Key-value pair structure; keys must be immutable types.
2. Values are accessed directly via keys, resulting in extremely high lookup efficiency.
3. Supports dynamic addition, deletion, and modification of key-value pairs.
Set Sets are created using curly braces { } or the set() function and store unique elements in an unordered collection. They are specifically designed to handle sets of unique data. 1. Automatically removes duplicate elements.
2. Elements are stored in an unordered manner; indexed access is not supported.
3. Supports mathematical set operations (union, intersection, difference, etc.).
4. Elements must be immutable types.

List

Blocks Note
image Assigning a list to a variable is equivalent to naming the list.
image Initialization list.
image Clear the data from the list.
image Get the length of the list.
image Determine whether the list is empty.
image Use the separator "," to join the elements in a list. For example, if the list contains ["a", "b", "c"], combining them with the separator ";" results in: a;b;c
image Connect text characters into a list using the delimiter ",". For example: If the contents of the list are [a-b-c], using the delimiter "-" to create the list results in: ["a", "b", "c"]
image Use an index to access values in the list; the first element of the list has an index of 0.
image Retrieve a specific element in a list by its index; you can retrieve it in ascending or descending order.
image Insert an element into the list at a specified position using an index.
image Modify the value at a specified position in the list using an index.
image Delete the corresponding element from the list using its index.
image Add the element to the end of the list.
image Concatenate the two lists into a single list.
image Convert the tuple to a list.
image Given the content of an element, find its corresponding index.
image Sort the elements in the list in ascending or descending order. Sorting options: numeric, alphabetical, or case-insensitive.
image Check whether a specific element is present in the list.

Tuple

Blocks Note
image Assigning a tuple to a variable is equivalent to naming the tuple.
image Initialize the tuple.
image Get the minimum, maximum, or length of a tuple.
image Determine whether a tuple contains a specific character.
image Use an index to retrieve values from a tuple.
image Retrieve a specific range of elements from the tuple by specifying the start and end indices.
image Convert the list to a tuple.

Dictionary

Blocks Note
image Assigning a dictionary to a variable is equivalent to naming the dictionary.
image Initialize the dictionary.
image Get the value of a dictionary key.
image Modify the value of a dictionary key.
image When you delete a key from a dictionary, the corresponding value is also removed.
image Clear the dictionary.
image Determine whether a specific key is present in the dictionary.
image Get the length of the dictionary.
image Returns a list containing all the keys or values in the dictionary.

Set

Blocks Note
image Assigning a collection to a variable is equivalent to naming the collection.
image Initialize the set.
image Initialize an empty set.
image For elements in two sets, find their intersection, union, or difference.
image Remove the command content from the collection.
image Clear the set.
image Copy the set.
image Determine whether the input value is in the set.
image Add an iterable object to the collection.
image Add a single element to the set.
image Determine whether one set is a subset or a superset of another set.
image Find the intersection, union, and difference of two sets.
image Get the length of the collection.
image Return a random item and remove it from the set.