Lists are used to store multiple items in a single variable.
Lists are one of the most commonly used data types in Python.
Creating a List
Lists are created using square brackets [].
Example:
fruits = ["Apple", "Banana", "Mango"]
print(fruits)
Output:
['Apple', 'Banana', 'Mango']
List Items
List items:
- Are ordered
- Are changeable
- Allow duplicate values
Example:
fruits = ["Apple", "Banana", "Apple"]
print(fruits)
Output:
['Apple', 'Banana', 'Apple']
List Length
Use the len() function to find the number of items in a list.
Example:
fruits = ["Apple", "Banana", "Mango"]
print(len(fruits))
Output:
3
List Data Types
A list can contain different data types.
Example:
items = ["John", 25, True, 10.5]
print(items)
Output:
['John', 25, True, 10.5]
Access List Items
List items are indexed starting from 0.
Example:
fruits = ["Apple", "Banana", "Mango"]
print(fruits[0])
Output:
Apple
Negative Indexing
Negative indexing starts from the end.
Example:
fruits = ["Apple", "Banana", "Mango"]
print(fruits[-1])
Output:
Mango
Range of Indexes
You can access a range of items using slicing.
Example:
fruits = ["Apple", "Banana", "Mango", "Orange"]
print(fruits[1:3])
Output:
['Banana', 'Mango']
Change List Items
Lists are changeable.
Example:
fruits = ["Apple", "Banana", "Mango"]
fruits[1] = "Orange"
print(fruits)
Output:
['Apple', 'Orange', 'Mango']
Add List Items
Use the append() method to add an item.
Example:
fruits = ["Apple", "Banana"]
fruits.append("Mango")
print(fruits)
Output:
['Apple', 'Banana', 'Mango']
Insert Items
Use the insert() method to add an item at a specific position.
Example:
fruits = ["Apple", "Mango"]
fruits.insert(1, "Banana")
print(fruits)
Output:
['Apple', 'Banana', 'Mango']
Remove List Items
Use the remove() method.
Example:
fruits = ["Apple", "Banana", "Mango"]
fruits.remove("Banana")
print(fruits)
Output:
['Apple', 'Mango']
Remove by Index
Use the pop() method.
Example:
fruits = ["Apple", "Banana", "Mango"]
fruits.pop(1)
print(fruits)
Output:
['Apple', 'Mango']
Loop Through a List
Example:
fruits = ["Apple", "Banana", "Mango"]
for fruit in fruits:
print(fruit)
Output:
Apple
Banana
Mango
Check if Item Exists
Use the in keyword.
Example:
fruits = ["Apple", "Banana", "Mango"]
print("Apple" in fruits)
Output:
True
Copy a List
Example:
fruits = ["Apple", "Banana"]
new_list = fruits.copy()
print(new_list)
Output:
['Apple', 'Banana']
Join Lists
Example:
list1 = ["Apple", "Banana"]
list2 = ["Mango", "Orange"]
list3 = list1 + list2
print(list3)
Output:
['Apple', 'Banana', 'Mango', 'Orange']
List Constructor
You can also create a list using the list() constructor.
Example:
fruits = list(("Apple", "Banana", "Mango"))
print(fruits)
Output:
['Apple', 'Banana', 'Mango']
Summary
- Lists store multiple items in a single variable.
- Lists are ordered, changeable, and allow duplicates.
- Use indexing to access items.
- Use
append()andinsert()to add items. - Use
remove()andpop()to delete items. - Use loops to iterate through lists.
- Use
len()to get the number of items. - Lists can contain different data types.