---Advertisement---

Python List Comprehension, Append, Sort, and Length Explained with Best Examples 2025

By Bhavani

Updated On:

---Advertisement---
 Python List Comprehension

👉Tutorial-2: How to Calculate Average of List in Python
👉Tutorial-3: Python count
👉Tutorial-4:  How to Remove Duplicates
👉Tutorial-5: How to Remove Elements from a Python
👉Tutorial-6:  Python List index
👉Tutorial-7: Python sort
👉Tutorial-8:  Python append

Python List Comprehension: Python lists are one of the most versatile data structures in Python, allowing flexible data storage and manipulation. Python List Comprehension This guide explores Python list functions like append, sort, length, and the powerful list comprehension method with practical examples.


Python List Comprehension: A Python list is a data structure that holds multiple items in a single variable. It can store integers, strings, floating-point numbers, or a combination of these. Lists are mutable, meaning their content can be changed after creation. Lists are defined using square brackets ([]).

python

numbers = [1, 2, 3, 8, 33]

animals = [‘dog’, ‘cat’, ‘goat’]

python

mixed_list = [2, ‘cat’, 34.33, ‘Travis’]


Python uses indexing to access list elements. Indexing starts at 0.

python

list = [3, 22, 30, 5.3, 20]

print(list[0])  # Output: 3

print(list[-1])  # Output: 20


List slicing allows you to extract specific elements from a list.

python

list = [3, 22, 30, 5.3, 20]

print(list[1:3])  # Output: [22, 30]

print(list[:4])   # Output: [3, 22, 30, 5.3]

print(list[2:-1]) # Output: [30, 5.3]


Lists can be updated by assigning new values to specific indices.

python

numbers = [2, 5, 9, 20, 27]

numbers[1] = 10

print(numbers)  # Output: [2, 10, 9, 20, 27]


Python provides three methods to delete list elements:

  • remove() – Deletes by value.
  • pop() – Deletes by index.
  • del – Deletes specific elements by index or the entire list.

python

values = [3, 5, 7, 8, 9, 20]

values.remove(3)

print(values)  # Output: [5, 7, 8, 9, 20]

values.pop(1)

print(values)  # Output: [5, 8, 9, 20]

del values[2]

print(values)  # Output: [5, 8, 20]


To add new elements to the end of a list, use the .append() method.

python

items = [3, 5, 7]

items.append(‘apple’)

print(items)  # Output: [3, 5, 7, ‘apple’]


Python provides powerful list functions for easy data manipulation:

  • len() – Returns the length of a list.
  • max() – Returns the maximum element.
  • min() – Returns the minimum element.
  • sum() – Returns the total of all numerical elements.

python

values = [2, 5, 10]

print(len(values))  # Output: 3

print(max(values))  # Output: 10

print(min(values))  # Output: 2

print(sum(values))  # Output: 17


Lists can be sorted in ascending or descending order using .sort().

python

values = [1, 7, 9, 3, 5]

values.sort()  # Ascending

print(values)  # Output: [1, 3, 5, 7, 9]

values.sort(reverse=True)  # Descending

print(values)  # Output: [9, 7, 5, 3, 1]


List comprehensions provide a concise way to create lists using a single line of code.

python

squares = [x**2 for x in range(1, 10)]

print(squares)

# Output: [1, 4, 9, 16, 25, 36, 49, 64, 81]

List comprehensions improve code readability and performance, making them a preferred choice in Python programming.


Python’s for loop efficiently iterates through lists.

python

numbers = [10, 20, 30, 40]

for num in numbers:

    print(num + 5)

# Output: 15, 25, 35, 45


✅ Python lists are versatile and widely used.
✅ Use .append() to add elements and .sort() to arrange data efficiently.
✅ List comprehensions simplify code and improve readability.
✅ Use list slicing, updating, and deletion methods to manipulate lists effectively.

Flask vs Django

Download Python

Leave a Comment

Index