
Getting Started With Python’s list Data Type
Getting Started With Python’s list Data Type 관련


Python’s list
is a flexible, versatile, powerful, and popular built-in data type. It allows you to create variable-length and mutable sequences of objects. In a list, you can store objects of any type. You can also mix objects of different types within the same list, although list elements often share the same type.
Note
Throughout this tutorial, you’ll use the terms items, elements, and values interchangeably to refer to the objects stored in a list.
Some of the more relevant characteristics of list
objects include being:
- Ordered: They contain elements or items that are sequentially arranged according to their specific insertion order.
- Zero-based: They allow you to access their elements by indices that start from zero.
- Mutable: They support in-place mutations or changes to their contained elements.
- Heterogeneous: They can store objects of different types.
- Growable and dynamic: They can grow or shrink dynamically, which means that they support the addition, insertion, and removal of elements.
- Nestable: They can contain other lists, so you can have lists of lists.
- Iterable: They support iteration, so you can traverse them using a loop or comprehension while you perform operations on each of their elements.
- Sliceable: They support slicing operations, meaning that you can extract a series of elements from them.
- Combinable: They support concatenation operations, so you can combine two or more lists using the concatenation operators.
- Copyable: They allow you to make copies of their content using various techniques.
Lists are sequences of objects. They’re commonly called containers or collections because a single list can contain or collect an arbitrary number of other objects.
Note
In Python, lists support a rich set of operations that are common to all sequence types, including tuples, strings, and ranges. These operations are known as common sequence operations. Throughout this tutorial, you’ll learn about several operations that fall into this category.
In Python, lists are ordered, which means that they keep their elements in the order of insertion:
colors = [
"red",
"orange",
"yellow",
"green",
"blue",
"indigo",
"violet"
]
colors
#
# ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
The items in this list are strings representing colors. If you access the list object, then you’ll see that the colors keep the same order in which you inserted them into the list. This order remains unchanged during the list’s lifetime unless you perform some mutations on it.
You can access an individual object in a list by its position or index in the sequence. Indices start from zero:
colors[0]
#
# 'red'
colors[1]
#
# 'orange'
colors[2]
#
# 'yellow'
colors[3]
#
# 'green'
Positions are numbered from zero to the length of the list minus one. The element at index 0
is the first element in the list, the element at index 1
is the second, and so on.
Lists can contain objects of different types. That’s why lists are heterogeneous collections:
[42, "apple", True, {"name": "John Doe"}, (1, 2, 3), [3.14, 2.78]]
This list contains objects of different data types, including an integer number, string, Boolean value, dictionary, tuple, and another list. Even though this feature of lists may seem cool, in practice you’ll find that lists typically store homogeneous data.
Note
One of the most relevant characteristics of lists is that they’re mutable data types. This feature deeply impacts their behavior and use cases. For example, mutability implies that lists aren’t hashable, so you can’t use them as dictionary keys. You’ll learn a lot about how mutability affects lists throughout this tutorial. So, keep reading!
Okay! That’s enough for a first glance at Python lists. In the rest of this tutorial, you’ll dive deeper into all the above characteristics of lists and more. Are you ready? To kick things off, you’ll start by learning the different ways to create lists.