0
0
Pythonprogramming~5 mins

Why lists are used in Python

Choose your learning style9 modes available
Introduction

Lists help us keep many items together in one place. They make it easy to store, find, and change groups of things.

You want to keep a shopping list and add or remove items.
You need to store scores of players in a game and update them.
You want to remember names of friends and print them all.
You have a collection of tasks to do and want to check them off.
You want to loop through a group of numbers to do math.
Syntax
Python
my_list = [item1, item2, item3]

# Example:
numbers = [1, 2, 3, 4, 5]

Lists use square brackets [] to hold items.

Items can be different types like numbers, words, or even other lists.

Examples
This shows an empty list with no items.
Python
empty_list = []
print(empty_list)
A list can have just one item.
Python
single_item_list = [42]
print(single_item_list)
Lists can hold different types of items together.
Python
mixed_list = [1, 'apple', 3.14]
print(mixed_list)
Lists can even hold other lists inside them.
Python
nested_list = [1, [2, 3], 4]
print(nested_list)
Sample Program

This program shows how to create a list, add and remove items, and get an item by position.

Python
fruits = ['apple', 'banana', 'cherry']
print('Original list:', fruits)

# Add a fruit
fruits.append('date')
print('After adding date:', fruits)

# Remove a fruit
fruits.remove('banana')
print('After removing banana:', fruits)

# Access first fruit
print('First fruit:', fruits[0])
OutputSuccess
Important Notes

Lists keep items in order, so you can get them by their position.

Adding or removing items is easy and fast at the end of the list.

Common mistake: forgetting that list positions start at 0, not 1.

Summary

Lists store many items together in one variable.

They keep the order of items and let you add, remove, or change them.

Lists are useful whenever you need to work with groups of things.