Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Python

Mastering Python Lists: Append, Sort, Length & List Comprehensions (Practical Guide)

What is a Python List?

A Python list is a versatile container that can hold objects of any type—integers, strings, floats, or even other lists. It is denoted by square brackets [], distinguishing it from tuples, which use parentheses (). Unlike tuples, lists are mutable: you can add, remove, or change elements after creation.

In this guide you’ll learn:

Examples of Python Lists

Lists can be homogeneous (all elements of the same type) or heterogeneous (mixed types). Here are clear examples:

# Homogeneous lists
int_list      = [1, 2, 3, 8, 33]
animal_list   = ['dog', 'cat', 'goat']
name_list     = ['John', 'Travis', 'Sheila']
float_list    = [2.2, 4.5, 9.8, 10.4]

# Heterogeneous lists
mixed_list_1  = [2, 'cat', 34.33, 'Travis']
mixed_list_2  = [2.22, 33, 'pen']

Accessing Values Within Lists

Python lists are zero‑based indexed. The negative index -1 refers to the last element.

items = [3, 22, 30, 5.3, 20]
print(items[0])   # 3
print(items[1])   # 22
print(items[-1])  # 20

Python List Slicing

Slicing extracts a sub‑sequence using start:stop syntax, where stop is exclusive.

items = [3, 22, 30, 5.3, 20]
print(items[:])      # All elements
print(items[1:3])    # [22, 30]
print(items[:4])     # [3, 22, 30, 5.3]
print(items[2:-1])   # [30, 5.3]

Updating Lists

Modify an element by assigning a new value to its index.

subjects = ['physics', 'chemistry', 'mathematics']
subjects[0] = 'biology'
print(subjects)  # ['biology', 'chemistry', 'mathematics']

Deleting List Elements

Three common approaches:

nums = [3, 5, 7, 8, 9, 20]
nums.remove(3)   # [5, 7, 8, 9, 20]
nums.pop(2)      # removes 8
# del nums[0]   # would delete 5

Appending List Elements

The append() method adds an item to the end of the list.

values = [3, 5, 7, 8, 9, 20]
values.append(3.33)
values.append('cats')
print(values)  # [3, 5, 7, 8, 9, 20, 3.33, 'cats']

Built‑in List Functions and Methods

values = [1, 7, 9, 3, 5]
values.sort()
print(values)  # [1, 3, 5, 7, 9]
values.sort(reverse=True)
print(values)  # [9, 7, 5, 3, 1]

Looping Through Lists

Iterate over elements with a for loop, optionally modifying each item or building a new list.

nums = [10, 20, 30, 40, 50, 60, 70]
# Add 5 to each element (note: this does not modify the original list)
for n in nums:
    print(n + 5)

# Build a new list from a slice
new_list = []
for n in nums[2:]:
    new_list.append(n)
print(new_list)  # [30, 40, 50, 60, 70]

List Comprehensions

List comprehensions offer a concise syntax for creating lists from iterables. They are faster and more readable than equivalent for loops.

# Squares of numbers 1 through 9
squares = [x**2 for x in range(1, 10)]
print(squares)  # [1, 4, 9, 16, 25, 36, 49, 64, 81]

Summary

Python

  1. Python List Operations: Creation, Access, Modification, and Advanced Techniques
  2. Python len(): A Practical Guide to Measuring Object Lengths
  3. Mastering Python's range() Function: From Basics to Advanced Use Cases
  4. Python and MySQL: A Practical Guide to Database Connectivity, Creation, and CRUD Operations
  5. Calculating Averages in Python: A Practical Guide
  6. Python list.count(): Expert Guide with Practical Examples
  7. How to Remove Duplicate Elements from a Python List
  8. How to Remove Items from a Python List: remove(), pop(), clear(), and del
  9. Python List index() – How to Find Element Positions with Practical Examples
  10. Mastering Python Lists: A Comprehensive Guide