• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • All
  • About
Python | Lists
  1. Notes
  2. Creating & Accessing Lists
  3. Adding & Modifying Items
  4. Removing Items
  5. Sorting & Reversing
  6. Searching & Indexing
  7. Looping & Iteration
  8. List Comprehensions
  9. Slicing & Copying
  10. Other Lists functions

  1. Notes
    A list is a mutable collection of ordered items.
    Items in the list can be of any type, including mixed types within the same list: strings, numbers, dictionaries, lists, objects, ...
    List allow duplicates and elements can be added or removed dynamically.
  2. Creating & Accessing Lists
    • Creating basic lists
      Use square brackets ([]) to create a list.
      Use commas to separate items in the list.
    • Accessing items in a list
      To access an item in a list, you use square brackets ([]) with the index of that item in the list.
      The index of the first item in the list is 0, and the next one is 1, ...
      You can access items in a list from the last index using negative numbers.
      The index of the last item is -1, and the one before the last one is -2, ...
      If the list is empty, using any index to access an item in the list will return an error.
      Using an index that's higher than the list size will return an error.
  3. Adding & Modifying Items
    • Appending items to the end of a list

    • Inserting items into a list at specific position

    • Adding items from list

    • Adding lists (new list created)

    • Modifying items in a list

    • Modifying multiple items with slicing

    • Inserting multiple items with slicing
  4. Removing Items
    • Removing an item using the del method

    • Removing item using the del method and slicing

    • Removing the last item in a list

    • Removing an item from a specific position in a list

    • Removing an item by its value from a list

    • Removing all items
  5. Sorting & Reversing
    • Sorting permanently a list: sort()

    • Sorting temporarily a list: sorted()

    • Sorting with key function

    • Reversing items of a list (no sorting)

    • Reversing items of a list using slicing (no sorting)
  6. Searching & Indexing
    • Finding first occurrence of an element

    • Membership
  7. Looping & Iteration
    • Looping through a list
      Output:

    • Using the range() function
      The range function has two parameters.
      The value of the first parameter is inclusive in the range.
      The value of the second parameter is exclusive from the range.

      Output:

    • Making a list of numbers using range() function

    • The enumerate() function
      The enumerate() function returns a key-value pair for each item in a list.
      the key is the index of the item in the list and the value is the item. Output:

    • Starting enumerate from a specific number
      Output:

    • Using zip() to iterate over multiple lists
      Output:
  8. List Comprehensions
    • List comprehensions

    • List comprehension with conditional filtering

    • List comprehension with conditional expression

    • List comprehension with string methods

    • Nested list comprehension
  9. Slicing & Copying
    • Slicing a list ([start:end:step])

      You can specify the index of the first item (inclusive) and the last item (exclusive):

      You can skip the index of the first item (defaults to 0)
      You can skip the index of the last item (defaults to the length of the list):

      You can skip the index of the first and last item (useful to copy a list): index of the first item default to 0 and the last defaults to the length of the list
      To get the slice of the last items, you can use a negative index:

      You can decide how many items to skip between items in the specified range by setting a third index in the brackets:

    • Looping through a slice
      Output:

    • Copying lists

      Copy by reference:

      Copy by value:
      Basic types don't affect the original values.
      Nested objects are still references to the items in the original list and hence updates affect the original value.
      Replacing the entire nested object reference does NOT affect the original object.

      Deep copy: create complete new list including nested objects
  10. Other Lists functions
© 2025  mtitek