What is a List in Python?

A list is an ordered collection of items. You can think of it like a shopping list, where you have items arranged in a certain order. Each item in the list has an index (a position number), starting from 0.

Example:

my_list = [10, 20, 30, 40]

This list has four items: 10, 20, 30, and 40. The index for 10 is 0, 20 is 1, 30 is 2, and 40 is 3.

List Methods in Python

Let’s go over the list methods you can use.


1. append()

The append() method adds a single element to the end of the list.

Syntax:

list.append(element)

Example:

my_list = [1, 2, 3]
my_list.append(4)  # Adding 4 to the list
print(my_list)

Output:

[1, 2, 3, 4]

2. extend()

The extend() method adds all elements from an iterable (like another list) to the end of the current list.

Syntax:

list.extend(iterable)

Example:

my_list = [1, 2]
my_list.extend([3, 4])  # Adding two items from another list
print(my_list)

Output:

[1, 2, 3, 4]

3. insert()

The insert() method allows you to insert an element at a specific index.

Syntax:

list.insert(index, element)

Example:

my_list = [1, 2, 4]
my_list.insert(2, 3)  # Insert 3 at index 2
print(my_list)

Output:

[1, 2, 3, 4]

4. remove()

The remove() method removes the first occurrence of a specified element.

Syntax:

list.remove(element)

Example:

my_list = [1, 2, 3, 2]
my_list.remove(2)  # Removes the first 2
print(my_list)

Output:

[1, 3, 2]

5. pop()

The pop() method removes and returns the item at the given index. If no index is provided, it removes and returns the last item.

Syntax:

list.pop(index)

Example:

my_list = [1, 2, 3]
removed_item = my_list.pop()  # Removes the last item
print(removed_item)  # The item that was removed
print(my_list)  # Remaining items

Output:

3
[1, 2]

6. index()

The index() method returns the index of the first occurrence of a specified value.

Syntax:

list.index(element)

Example:

my_list = [10, 20, 30]
index = my_list.index(20)  # Find the index of 20
print(index)

Output:

1

7. count()

The count() method returns the number of occurrences of an element in the list.

Syntax:

list.count(element)

Example:

my_list = [1, 2, 2, 3, 2]
count = my_list.count(2)  # Count how many times 2 appears
print(count)

Output:

3

8. sort()

The sort() method sorts the list in ascending order. You can also pass a parameter to sort in descending order.

Syntax:

list.sort(reverse=False)

Example:

my_list = [4, 2, 3, 1]
my_list.sort()  # Sorting in ascending order
print(my_list)

Output:

[1, 2, 3, 4]

9. reverse()

The reverse() method reverses the order of the elements in the list.

Syntax:

list.reverse()

Example:

my_list = [1, 2, 3]
my_list.reverse()  # Reversing the list
print(my_list)

Output:

[3, 2, 1]

10. clear()

The clear() method removes all items from the list, leaving it empty.

Syntax:

list.clear()

Example:

my_list = [1, 2, 3]
my_list.clear()  # Clear all elements from the list
print(my_list)

Output:

[]

11. copy()

The copy() method returns a shallow copy of the list. This means a new list with the same elements.

Syntax:

new_list = list.copy()

Example:

my_list = [1, 2, 3]
new_list = my_list.copy()  # Copying the list
print(new_list)

Output:

[1, 2, 3]