Nearby lessons
24 of 108Python - List Data Type
Introduction
If we want to store a group of values as a single object where:
- Insertion order is preserved.
- Duplicate values are allowed.
- Different data types (heterogeneous values) are allowed.
Then we should use the list data type.
Features of List Data Type
- Insertion order is preserved.
- Duplicate values are allowed.
- Heterogeneous values are allowed.
- Growable in nature.
- Mutable.
- Elements are enclosed within square brackets
[ ].
Example of List
main.py
[10, 10.5, 'durga', True, 10]
Explanation
The above list contains different types of values.
| Value | Data Type |
|---|---|
| 10 | int |
| 10.5 | float |
| 'durga' | str |
| True | bool |
This shows that lists support heterogeneous values.
Duplicate Values Allowed
Lists allow duplicate values.
Example - Duplicate Values
main.py
[10, 20, 10, 30]
Accessing List Elements
We can access list elements using indexes.
Lists support both positive and negative indexing.
Example - Indexing
main.py
10 40
Index Representation
| Element | 10 | 20 | 30 | 40 |
|---|---|---|---|---|
| Positive Index | 0 | 1 | 2 | 3 |
| Negative Index | -4 | -3 | -2 | -1 |
List Slicing
Lists support slicing.
Syntax:
list[start:end]
Example - List Slicing
main.py
[20, 30]
Modifying List Elements
Lists are mutable.
This means list elements can be changed after creation.
Example - Modifying Elements
main.py
[100, 20, 30, 40]
Iterating List Elements
We can use a loop to iterate through all list elements.
Example - Iteration
main.py
10 20 30 40
List is Growable in Nature
The size of a list can be increased or decreased dynamically.
Adding Elements Using append()
main.py
[10, 20, 30, 'durga']
Removing Elements Using remove()
main.py
[10, 30]
List Repetition Using * Operator
We can repeat list elements using the * operator.
Example - List Repetition
main.py
[10, 30, 'durga', 10, 30, 'durga']
Nested List
A list can contain another list.
Example - Nested List
main.py
[10, 20, [30, 40]] [30, 40]
Length of List
Use the len() function to find the number of elements in a list.
Example - len() Function
main.py
3
Membership Operators
We can check whether an element exists in a list using:
innot in
Example - Membership Operators
main.py
True False
Important Notes
- Lists are mutable.
- Duplicate values are allowed.
- Different data types can be stored together.
- Insertion order is preserved.
- Lists are growable.
Difference Between List and Tuple
| List | Tuple |
|---|---|
| Mutable | Immutable |
Uses [ ] |
Uses ( ) |
| Growable | Fixed Size |
Example - List
main.py
[100, 20]
Example - Tuple
main.py
TypeError
Key Points
- Lists store multiple values in a single object.
- Lists preserve insertion order.
- Lists allow duplicate and heterogeneous values.
- Lists support indexing, slicing, and iteration.
- Lists are mutable and growable.
- Common list methods include
append()andremove().
Quick Summary
| Feature | Description |
|---|---|
| Data Type | list |
| Mutable | Yes |
| Duplicates Allowed | Yes |
| Heterogeneous Values | Yes |
| Insertion Order | Preserved |
| Growable | Yes |
| Symbol | [ ] |