Skip to content

Instantly share code, notes, and snippets.

@raj1rana
Created January 18, 2025 16:18
Show Gist options
  • Save raj1rana/92f95d90ae955d03be73b31d589c97a7 to your computer and use it in GitHub Desktop.
Save raj1rana/92f95d90ae955d03be73b31d589c97a7 to your computer and use it in GitHub Desktop.

Python provides a wide range of built-in functions and methods to work with lists. Here’s a comprehensive list of them, grouped by functionality:


1. Adding Elements

  • append(): Adds a single element to the end of the list.

    fruits = ["apple", "banana"]
    fruits.append("cherry")
    print(fruits)  # Output: ["apple", "banana", "cherry"]
  • extend(): Adds elements from another iterable (e.g., list, tuple) to the end of the list.

    fruits = ["apple", "banana"]
    fruits.extend(["cherry", "date"])
    print(fruits)  # Output: ["apple", "banana", "cherry", "date"]
  • insert(): Inserts an element at a specified index.

    fruits = ["apple", "banana"]
    fruits.insert(1, "cherry")
    print(fruits)  # Output: ["apple", "cherry", "banana"]

2. Removing Elements

  • remove(): Removes the first occurrence of a specified value.

    fruits = ["apple", "banana", "cherry"]
    fruits.remove("banana")
    print(fruits)  # Output: ["apple", "cherry"]
  • pop(): Removes and returns an element at a specified index (default is the last element).

    fruits = ["apple", "banana", "cherry"]
    removed = fruits.pop(1)
    print(removed)  # Output: "banana"
    print(fruits)   # Output: ["apple", "cherry"]
  • clear(): Removes all elements from the list.

    fruits = ["apple", "banana"]
    fruits.clear()
    print(fruits)  # Output: []

3. Querying Elements

  • index(): Returns the index of the first occurrence of a specified value.

    fruits = ["apple", "banana", "cherry"]
    idx = fruits.index("banana")
    print(idx)  # Output: 1
  • count(): Returns the number of occurrences of a specified value.

    fruits = ["apple", "banana", "apple"]
    cnt = fruits.count("apple")
    print(cnt)  # Output: 2

4. Sorting and Reversing

  • sort(): Sorts the list in ascending order (or descending with reverse=True).

    numbers = [3, 1, 4, 2]
    numbers.sort()
    print(numbers)  # Output: [1, 2, 3, 4]

    To sort in descending order:

    numbers.sort(reverse=True)
    print(numbers)  # Output: [4, 3, 2, 1]
  • reverse(): Reverses the elements of the list in place.

    fruits = ["apple", "banana", "cherry"]
    fruits.reverse()
    print(fruits)  # Output: ["cherry", "banana", "apple"]

5. Copying

  • copy(): Returns a shallow copy of the list.
    fruits = ["apple", "banana"]
    new_fruits = fruits.copy()
    print(new_fruits)  # Output: ["apple", "banana"]

6. List Comprehension (Special Case)

  • List comprehension: A concise way to create lists based on existing iterables.
    squares = [x**2 for x in range(5)]
    print(squares)  # Output: [0, 1, 4, 9, 16]

7. General Functions Applicable to Lists

  • len(): Returns the number of elements in the list.

    fruits = ["apple", "banana"]
    print(len(fruits))  # Output: 2
  • max(): Returns the maximum value in the list.

    numbers = [3, 1, 4, 2]
    print(max(numbers))  # Output: 4
  • min(): Returns the minimum value in the list.

    numbers = [3, 1, 4, 2]
    print(min(numbers))  # Output: 1
  • sum(): Returns the sum of all numeric elements in the list.

    numbers = [3, 1, 4, 2]
    print(sum(numbers))  # Output: 10
  • any(): Returns True if any element in the list is truthy.

    values = [0, False, 3]
    print(any(values))  # Output: True
  • all(): Returns True if all elements in the list are truthy.

    values = [1, True, 3]
    print(all(values))  # Output: True

8. Joining and Splitting

  • join(): Joins elements of a list into a string (only for strings).
    fruits = ["apple", "banana", "cherry"]
    result = ", ".join(fruits)
    print(result)  # Output: "apple, banana, cherry"

You can use these functions and methods to effectively manipulate and query lists in Python.****

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment