Fundamentals 6 min read

Master Python List Operations: Indexing, Slicing, and Essential Methods

This guide explains how to access, modify, and manipulate Python lists using indexing, slicing, and a variety of built‑in methods such as append, count, extend, insert, pop, remove, reverse, and sort, with clear code examples and expected results.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Python List Operations: Indexing, Slicing, and Essential Methods

Basic List Operations

Like strings, list elements can be accessed by index and support slicing.

list1 = [1, 2, 3, 4, 5]
print(list[1])           # retrieve the second element
print(list[-1])          # retrieve the last element
print(list[2:4])         # retrieve the third and fourth elements
print(list[1:])          # retrieve the second element and all following elements
# Result:
2
5
[3, 4]
[2, 3, 4, 5]

Modifying Lists

Lists are mutable, allowing element reassignment or deletion.

list1 = [1, 2, 3, 4, 5]
list1[1] = 100    # reassign the second element
print(list1)
list1[2] = ["I", "Love", "Python"]  # replace the third element with a new list
print(list1)
del list1[0]    # delete the first element
print(list1)
"""Result:
[1, 100, 3, 4, 5]
[1, 100, ['I', 'Love', 'Python']]
[100, ['I', 'Love', 'Python'], 4, 5]
"""

List Methods

Python provides many built‑in methods for common list operations.

append() – adds a new element to the end of the list.

list1 = [1, 2, 3]
list1.append(4)
print(list1)
"""Result:
[1, 2, 3, 4]
"""

count() – returns the number of occurrences of a specific element.

list1 = ["I", "Love", "Python", "You", "Love", "Python", "Too"]
print(list1.count("Python"))
"""Result:
2
"""

extend() – appends another list to the end without changing the original list's memory address.

list1 = [1, 2, 3]
print(id(list1))
list1.extend([4, 5, 6])
print(list1)
print(id(list1))
"""Result:
42258827
[1, 2, 3, 4, 5, 6]
42258827
"""

index() – finds the first index of a value; raises an exception if the value is absent.

list1 = [1, 2, 3]
print(list1.index(2))
print(list1.index(5))
"""Result:
1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 5 is not in list
"""

insert() – inserts a value at a specified position.

list1 = [1, 2, 3]
list1.insert(1, 1.5)
print(list1)
"""Result:
[1, 1.5, 2, 3]
"""

pop() – removes and returns an element (default is the last one).

list1 = [1, 2, 3, 4, 5]
list1.pop()          # removes 5
list1.pop(1)         # removes element at index 1 (value 2)
print(list1)
"""Result:
[1, 3, 4]
"""

remove() – deletes the first occurrence of a specified value.

list1 = [1, 2, 3, 2, 3, 4]
list1.remove(2)
print(list1)
"""Result:
[1, 3, 2, 3, 4]
"""

reverse() – reverses the order of elements in place.

list1 = [1, 2, 3]
list1.reverse()
print(list1)
"""Result:
[3, 2, 1]
"""

sort() – sorts the list; the list’s memory address remains unchanged.

list1 = [2, 3, 1, 5, 3, 6]
print(id(list1))
list1.sort()
print(id(list1))
print(list1)
"""Result:
42257724
42257724
[1, 2, 3, 4, 5, 6]
"""

You can quickly copy a list with y = x[:] or obtain a sorted copy with y = sorted(x).

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ListSlicinglist-methods
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.