Python Lists


Lists in Python

A list is a collection of items that are ordered and mutable (meaning you can modify the elements). Lists can store elements of different types, such as integers, floats, strings, or even other lists.

Key Features of Lists:

  1. Ordered: The elements in a list have a defined order, and this order will not change unless explicitly modified.
  2. Mutable: You can change the elements of a list after it is created.
  3. Heterogeneous: A list can contain elements of different types.
  4. Dynamic: Lists can grow and shrink as needed.

List Syntax:

To define a list in Python, you use square brackets [], with elements separated by commas.

# Creating a list with different data types my_list = [1, 2.5, "Hello", [10, 20]]

Example of Common List Operations

  1. Creating a List:
# Creating a simple list fruits = ["apple", "banana", "cherry"] print(fruits) # Output: ['apple', 'banana', 'cherry']
  1. Accessing Elements:
    • You can access individual elements using an index (starting from 0).
# Accessing elements by index print(fruits[0]) # Output: 'apple' print(fruits[2]) # Output: 'cherry'
  1. Negative Indexing:
    • Negative indexing allows you to access elements from the end of the list.
# Access the last element print(fruits[-1]) # Output: 'cherry'
  1. Slicing a List:
    • You can access a portion of the list (called slicing).
# Slicing the list (start index is inclusive, end index is exclusive) print(fruits[0:2]) # Output: ['apple', 'banana']
  1. Modifying Elements:
    • Since lists are mutable, you can modify an element.
# Changing an element fruits[1] = "blueberry" print(fruits) # Output: ['apple', 'blueberry', 'cherry']
  1. Adding Elements:
    • You can add elements using append(), insert(), or extend() methods.
# Adding elements to a list fruits.append("orange") # Adds at the end print(fruits) # Output: ['apple', 'blueberry', 'cherry', 'orange'] # Inserting an element at a specific index fruits.insert(1, "mango") print(fruits) # Output: ['apple', 'mango', 'blueberry', 'cherry', 'orange']
  1. Removing Elements:
    • Elements can be removed using remove(), pop(), or del.
# Removing by value fruits.remove("blueberry") print(fruits) # Output: ['apple', 'mango', 'cherry', 'orange'] # Removing by index fruits.pop(2) # Removes the element at index 2 print(fruits) # Output: ['apple', 'mango', 'orange'] # Using del to remove an element by index del fruits[1] print(fruits) # Output: ['apple', 'orange']
  1. Checking Length of List:
    • You can find the number of elements in a list using len().
# Finding the length of the list print(len(fruits)) # Output: 2
  1. Looping Through a List:
    • You can loop over the elements in a list using a for loop.
# Iterating through a list for fruit in fruits: print(fruit) # Output: # apple # orange
  1. List Comprehension:
    • List comprehension is a concise way to create a new list by iterating over an existing list.
# Example of list comprehension numbers = [1, 2, 3, 4, 5] squared_numbers = [x**2 for x in numbers] print(squared_numbers) # Output: [1, 4, 9, 16, 25]

More List Methods:

  • sort(): Sorts the list in ascending order.
  • reverse(): Reverses the list.
  • index(): Returns the index of the first matched element.
  • count(): Returns the number of occurrences of a value.
# Sorting a list fruits = ["banana", "apple", "cherry", "date"] fruits.sort() print(fruits) # Output: ['apple', 'banana', 'cherry', 'date'] # Reversing a list fruits.reverse() print(fruits) # Output: ['date', 'cherry', 'banana', 'apple'] # Finding the index of an element print(fruits.index("banana")) # Output: 2 # Counting occurrences of an element print(fruits.count("apple")) # Output: 1

Summary:

  • A list is a versatile, mutable data structure that allows you to store a collection of ordered elements.
  • It can handle different data types and supports a variety of operations like adding, removing, and modifying elements.