
👉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 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.
👉What is a Python List?
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 List Comprehension Examples of Python Lists
- Homogeneous List (Same Data Types):
python
numbers = [1, 2, 3, 8, 33]
animals = [‘dog’, ‘cat’, ‘goat’]
- Heterogeneous List (Mixed Data Types):
python
mixed_list = [2, ‘cat’, 34.33, ‘Travis’]
👉Accessing Values in Lists
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
👉Python List Slicing
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]
👉Updating List Elements
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]
👉Deleting List Elements
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]
👉Appending Elements to a List
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 List Built-in Functions
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.
Example:
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
👉Python List Sorting
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 Comprehension in Python
List comprehensions provide a concise way to create lists using a single line of code.
Example: Creating a list of squares using list comprehension:
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.
👉Looping Through Lists
Python’s for loop efficiently iterates through lists.
Example:
python
numbers = [10, 20, 30, 40]
for num in numbers:
print(num + 5)
# Output: 15, 25, 35, 45
👉Key Takeaways
✅ 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.