Lesson 26 of 70 – Python Lists
37%

Python Lists

A list is one of the most commonly used data structures in Python. A list is used to store multiple values in a single variable.

Lists are ordered, changeable (mutable), and allow duplicate values.

Note: Python lists are written using square brackets [ ].
What is a List?

A list is a collection of multiple items stored in a single variable.

students = ["Amit", "Rahul", "Priya"]

print(students)
Output:
['Amit', 'Rahul', 'Priya']
Creating a List

A list is created using square brackets.

numbers = [10, 20, 30, 40, 50]

print(numbers)
Output:
[10, 20, 30, 40, 50]
List with Different Data Types

A Python list can contain different types of values.

data = [10, "Python", 25.5, True]

print(data)
Output:
[10, 'Python', 25.5, True]
List with Duplicate Values

Lists allow duplicate values.

numbers = [10, 20, 10, 30, 20]

print(numbers)
Output:
[10, 20, 10, 30, 20]
List Indexing

List elements are accessed using their index. Python list indexing starts from 0.

names = ["Amit", "Rahul", "Priya"]

print(names[0])
print(names[1])
print(names[2])
Output:
Amit
Rahul
Priya
Negative Indexing

Python also supports negative indexing. The last element has index -1.

names = ["Amit", "Rahul", "Priya"]

print(names[-1])
print(names[-2])
Output:
Priya
Rahul
Changing List Items

Lists are mutable, which means their elements can be changed after the list is created.

names = ["Amit", "Rahul", "Priya"]

names[1] = "Ravi"

print(names)
Output:
['Amit', 'Ravi', 'Priya']
List Length

The len() function returns the number of elements in a list.

numbers = [10, 20, 30, 40]

print(len(numbers))
Output:
4
Checking an Item in a List

The in operator checks whether an item exists in a list.

names = ["Amit", "Rahul", "Priya"]

print("Rahul" in names)
print("Ravi" in names)
Output:
True
False
Checking an Item Not in a List
numbers = [10, 20, 30]

print(50 not in numbers)
Output:
True
List Slicing

Slicing is used to get a portion of a list.

numbers = [10, 20, 30, 40, 50]

print(numbers[1:4])
Output:
[20, 30, 40]

The start index is included and the stop index is excluded.

List Slicing from Beginning
numbers = [10, 20, 30, 40, 50]

print(numbers[:3])
Output:
[10, 20, 30]
List Slicing to End
numbers = [10, 20, 30, 40, 50]

print(numbers[2:])
Output:
[30, 40, 50]
Negative Slicing
numbers = [10, 20, 30, 40, 50]

print(numbers[-3:])
Output:
[30, 40, 50]
Loop Through a List
names = ["Amit", "Rahul", "Priya"]

for name in names:
    print(name)
Output:
Amit
Rahul
Priya
List with While Loop
numbers = [10, 20, 30]

i = 0

while i < len(numbers):
    print(numbers[i])
    i += 1
Output:
10
20
30
Nested List

A list can contain other lists. This is called a nested list.

students = [
    ["Amit", 20],
    ["Rahul", 21],
    ["Priya", 19]
]

print(students)
Output:
[['Amit', 20], ['Rahul', 21], ['Priya', 19]]
Accessing Nested List
students = [
    ["Amit", 20],
    ["Rahul", 21]
]

print(students[0][0])
print(students[1][1])
Output:
Amit
21
List Concatenation

The + operator can be used to combine two lists.

list1 = [10, 20]
list2 = [30, 40]

result = list1 + list2

print(result)
Output:
[10, 20, 30, 40]
Repeating a List

The * operator can repeat the elements of a list.

numbers = [1, 2]

print(numbers * 3)
Output:
[1, 2, 1, 2, 1, 2]
Copying a List

The copy() method can be used to create a shallow copy of a list.

numbers = [10, 20, 30]

new_numbers = numbers.copy()

print(new_numbers)
Output:
[10, 20, 30]
List Assignment vs Copy

Simply assigning one list to another does not create an independent copy.

list1 = [10, 20, 30]

list2 = list1

list2[0] = 100

print(list1)
Output:
[100, 20, 30]

Both variables refer to the same list object.

List Constructor

The list() constructor can be used to create a list from another iterable.

text = "Python"

letters = list(text)

print(letters)
Output:
['P', 'y', 't', 'h', 'o', 'n']
List Unpacking

List elements can be assigned to multiple variables.

student = ["Rahul", 20, "Python"]

name, age, course = student

print(name)
print(age)
print(course)
Output:
Rahul
20
Python
Important List Properties
  • Lists are ordered.
  • Lists are changeable.
  • Lists allow duplicate values.
  • Lists can contain different data types.
  • Lists support positive and negative indexing.
  • Lists support slicing.
  • Lists can contain other lists.
  • Lists are written using square brackets.
Key Points
  • A list stores multiple values in one variable.
  • Lists are created using square brackets [ ].
  • List indexing starts from 0.
  • Negative indexing starts from -1.
  • Lists are mutable.
  • Lists allow duplicate values.
  • The len() function returns the number of elements.
  • List elements can be changed after creation.
  • Lists support slicing.
  • Lists can contain different data types.
  • Lists can be nested.

🧠 Quick Quiz

Question: Which brackets are used to create a list in Python?