• Home
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • Python
  • Ubuntu
  • Maven
  • Archived
  • About
Python | Lists
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.

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.

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:

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:

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):

Finding first occurrence of an element:

Membership:

Looping through a list:
Output:

Note that the variable "y" will be accessible after the loop finishes.
Note that the indentation is very important to limit the block of the instructions that are part of the loop.

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:

Returning the min, max, and sum of items of a list:

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:

List comprehensions:

List comprehension with conditional filtering:

List comprehension with conditional expression:

List comprehension with string methods:

Nested list comprehension:

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:

Lists functions:
© 2025  mtitek