A list is a data structure designed to store ordered sets of elements.
In this case, the word "ordered" means we store the structural elements in the order in which we added them.
Items in the list are indexed, meaning they have a number that tells you their position. The numbering always starts from zero and is always monotonous. It means that each subsequent item index increases by one — this numbering is also called continuous numbering. If you delete items at the beginning or middle of the list or insert items in the middle, Python recalculates the indexes and preserves the above numbering properties.
Immutability and modifiability
We already know that concatenating two lines results in a new line, and adding two numbers results in a number.
Tuples, which we looked at earlier:
- Allow you to group the elements and then break the group into components
- Can be combined into a larger tuple
- Don't allow you to change the composition of the elements — instead, create new tuples from the elements of the old ones
Earlier, we discussed many data types and structures, such as strings, numbers, Boolean values, and tuples. They are immutable, meaning that once we create them, they don't change.
Lists are the first data structure that can change its contents on the fly. These objects are called changeable objects. However, some list operations still create new lists based on old lists.
Are there any more questions? Ask them in the Discussion section.
The Hexlet support team or other students will answer you.
For full access to the course you need a professional subscription.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.