Junior — Middle
What properties and features are inherent to the list data structure in Python?
sobes.tech AI
Answer from AI
A list in Python is a mutable ordered collection of elements that can store objects of different types. The main properties and features of lists:
- Orderliness: elements have an index starting from 0, and the order of elements is preserved.
- Mutability: elements can be added, removed, and modified after the list is created.
- Heterogeneity: a list can contain objects of different types (numbers, strings, other lists, etc.).
- Support for slices: sublists can be obtained using slices.
- Dynamic size: a list can automatically expand when new elements are added.
Example of using a list:
fruits = ['apple', 'banana', 'cherry']
fruits.append('orange') # adding an element
print(fruits[1]) # banana
fruits[0] = 'pear' # changing an element
print(fruits)