What is a List?
A list in Python is a collection data type that allows you to store multiple values in a single variable. These values can be of any type: integers, strings, floats, booleans, or even other lists.
When Should You Use a List?
Use a list when:
- You want to store a group of items as a single unit. For example, storing the names of all students in a class.
- Order matters — the sequence in which elements are added to the list is preserved. This is useful when processing data in the same order as it was collected.
- You want to allow duplicate values. Lists support repeated elements, so values like
[10, 10, 20]
are valid. - You want the flexibility to add, remove, or modify the items later. Lists are mutable, which means they can grow or shrink dynamically as your program runs.
List Characteristics
Feature | Description |
Ordered | Yes. Elements are stored in the order they are inserted. Access by position is possible using indexing. |
Allows Duplicates | Yes. The same value can appear multiple times. This is helpful when data repetition has significance. |
Mutable | Yes. Lists can be changed after creation — you can add, remove, or modify elements using built-in methods like append() , remove() , or direct assignment. |
Indexing Supported | Yes. Both positive indexing (from the start) and negative indexing (from the end) are supported. |
Slicing Supported | Yes. You can extract sublists using slice notation like list[start:end] . |
Heterogeneous Elements | Yes. Lists can store a mix of different data types like [10, "Durga", 3.14, True] . |
Syntax | Lists are defined using square brackets [] and elements are separated by commas. Example: [1, 2, 3] |
Creating a List
You can create a list by placing values inside square brackets:
l = [10, "Durga", 10, 20, 30] print(l) # Output: [10, 'Durga', 10, 20, 30] print(type(l)) # Output: <class 'list'>
Accessing List Elements
You can access list elements using indexing. Indexes start from 0.
print(l[0]) # 10 (first element) print(l[-1]) # 30 (last element)
You can also access a range of values using slicing. Slicing returns a new list.
print(l[1:4]) # ['Durga', 10, 20]
Here, 1:4
means start at index 1 (inclusive) and go up to index 4 (exclusive).
List is Mutable
You can modify the contents of a list by directly assigning a new value to a specific index:
l[0] = 7777 print(l) # [7777, 'Durga', 10, 20, 30]
This demonstrates that lists are mutable, meaning their contents can be changed in place.
Adding Elements to a List
You can add elements to the end of a list using the append()
method:
l = [] l.append(10) l.append(20) l.append(30) l.append(40) print(l) # [10, 20, 30, 40]
You can add any type of value, and the order of insertion will be preserved.
Removing Elements from a List
Use the remove()
method to delete a specific element by value:
l.remove(30) print(l) # [10, 20, 40]
If the value occurs multiple times, only the first occurrence will be removed.
Creating an Empty List
To initialize a list with no elements:
empty_list = [] print(empty_list) # []
This is useful when you’re building a list dynamically through loops or conditionals.
List is Growable
Lists can dynamically grow or shrink as needed. You can add elements using append()
or insert()
, and remove them using remove()
or pop()
.
l.append(50) # Add new element l.remove(10) # Remove an element print(l) # [20, 40, 50]
This dynamic resizing makes lists extremely flexible for data manipulation.