LISTS
Lists in Python are ordered collections of items. They are mutable, which means you can change, add, and remove elements after the list has been created. Lists can contain elements of different data types, including integers, floats, strings, and even other lists. Here's how you can work with lists in Python:
Creating a List:
You can create a list by enclosing comma-separated values within square brackets []
.
my_list = [1, 2, 3, 4, 5]
Accessing Elements:
You can access individual elements of a list using indexing. Indexing starts from 0 for the first element and goes up to len(list) - 1
for the last element.
print(my_list[0]) # Output: 1
print(my_list[-1]) # Output: 5 (Negative indexing starts from the end)
Slicing:
You can also extract a sublist (slice) from a list using slicing notation [start:stop:step]
.
print(my_list[1:4]) # Output: [2, 3, 4]
print(my_list[:3]) # Output: [1, 2, 3] (Start is omitted, defaults to 0)
print(my_list[::2]) # Output: [1, 3, 5] (Step is 2, so every second element is selected)
Modifying Elements:
You can modify individual elements of a list by assigning new values to them.
my_list[0] = 10
print(my_list) # Output: [10, 2, 3, 4, 5]
Adding and Removing Elements:
You can add elements to the end of a list using the append()
method, and remove elements using the remove()
or pop()
method.
my_list.append(6)
print(my_list) # Output: [10, 2, 3, 4, 5, 6]
my_list.remove(3)
print(my_list) # Output: [10, 2, 4, 5, 6]
popped_element = my_list.pop(1)
print("Popped element:", popped_element) # Output: Popped element: 2
print(my_list) # Output: [10, 4, 5, 6]
List Operations:
You can perform various operations on lists, such as concatenation (+
), repetition (*
), and checking membership (in
).
list1 = [1, 2, 3]
list2 = [4, 5, 6]
concatenated_list = list1 + list2
print(concatenated_list) # Output: [1, 2, 3, 4, 5, 6]
repeated_list = list1 * 3
print(repeated_list) # Output: [1, 2, 3, 1, 2, 3, 1, 2, 3]
print(2 in list1) # Output: True
Lists are versatile and widely used in Python for storing and manipulating collections of data.
Lists in Python come with several built-in methods that allow you to perform various operations on them. Here's an overview of some commonly used list methods:
1. append()
:
Adds an element to the end of the list.
my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]
2. extend()
:
Appends the elements of another list to the end of the current list.
my_list = [1, 2, 3]
my_list.extend([4, 5, 6])
print(my_list) # Output: [1, 2, 3, 4, 5, 6]
3. insert()
:
Inserts an element at a specified position.
my_list = [1, 2, 3]
my_list.insert(1, 5)
print(my_list) # Output: [1, 5, 2, 3]
4. remove()
:
Removes the first occurrence of a specified value from the list.
my_list = [1, 2, 3, 2]
my_list.remove(2)
print(my_list) # Output: [1, 3, 2]
5. pop()
:
Removes and returns the element at the specified index. If no index is specified, removes and returns the last element.
my_list = [1, 2, 3]
popped_element = my_list.pop(1)
print(popped_element) # Output: 2
print(my_list) # Output: [1, 3]
6. index()
:
Returns the index of the first occurrence of a specified value.
my_list = [1, 2, 3, 2]
index = my_list.index(2)
print(index) # Output: 1
7. count()
:
Returns the number of occurrences of a specified value.
my_list = [1, 2, 3, 2]
count = my_list.count(2)
print(count) # Output: 2
8. sort()
:
Sorts the elements of the list in ascending order.
my_list = [3, 1, 2]
my_list.sort()
print(my_list) # Output: [1, 2, 3]
9. reverse()
:
Reverses the order of the elements in the list.
my_list = [1, 2, 3]
my_list.reverse()
print(my_list) # Output: [3, 2, 1]
These are just a few of the many methods available for working with lists in Python. They provide convenient ways to manipulate lists according to your requirements.