


Khusboo Tayal
Python Lists are one of the most versatile and widely used data types in Python. A list is a collection of ordered, changeable, and allow duplicate elements. Lists are perfect for storing multiple items in a single variable.
In this guide, you'll learn what lists are, how to use them, and common operations you can perform.
A list is an ordered collection of items, which can be of any data type—strings, numbers, or even other lists. Lists are defined using square brackets [].
fruits = ["apple", "banana", "cherry"]
You can create a list using:
my_list = [1, 2, 3, 4, 5] mixed_list = ["hello", 3.14, True, 42]
You can access items using their index, starting from 0:
print(fruits[0]) # Output: apple print(fruits[2]) # Output: cherry
You can also use negative indexing:
print(fruits[-1]) # Output: cherry
Lists are mutable—you can change items using their index:
fruits[1] = "blueberry" print(fruits) # ['apple', 'blueberry', 'cherry']
You can iterate over a list using a for loop:
for fruit in fruits:
print(fruit)
Use len() to find the number of elements:
print(len(fruits)) # Output: 3
fruits.append("mango")fruits.insert(1, "orange")
fruits.extend(["grape", "kiwi"])
fruits.remove("banana")fruits.pop()
fruits.clear()
You can get a sublist using slicing:
print(fruits[1:3]) # Elements at index 1 and 2 print(fruits[:2]) # First two elements print(fruits[-2:]) # Last two elements
A concise way to create lists:
squares = [x**2 for x in range(5)] print(squares) # Output: [0, 1, 4, 9, 16]
| Method | Description |
|---|---|
| append() | Adds an element at the end |
| insert() | Inserts an element at a given index |
| extend() | Adds elements from another list |
| remove() | Removes the first occurrence |
| pop() | Removes element by index |
| clear() | Clears the list |
| index() | Returns index of element |
| count() | Counts occurrences of element |
| sort() | Sorts the list |
| reverse() | Reverses the list |
| copy() | Returns a copy of the list |
Lists can contain other lists:
matrix = [[1, 2], [3, 4], [5, 6]] print(matrix[1][0]) # Output: 3
Python lists are dynamic—you don’t need to define their size in advance. You can add or remove items at any time.
Python Lists are powerful and flexible. They support various operations like adding, removing, sorting, slicing, and looping. Whether you're a beginner or an experienced developer, mastering Python lists is essential for efficient programming.
Practice with different list methods and try solving real-world problems using lists to strengthen your understanding.
Q1: Can Python lists contain different data types?
Yes, a single list can contain multiple data types.
Q2: Are Python lists ordered?
Yes, lists maintain the order of insertion.
Q3: How are Python lists different from arrays?
Python lists are more flexible and can hold multiple data types, unlike arrays which usually contain the same type.
Subscribe to our newsletter to get the latest Python tutorials, project ideas, and updates right to your inbox.
#python #pythonlists #pythondatatypes #listcomprehension #pythonforbeginners #learnpython #pythontutorial #pythoncode #pythonexamples #datastructures