List
A list is a data type that stores multiple\any data types in an ordered sequence. It is mutable and one of the most used data types in Python. You can store integers, floats, strings, and so on.
Example
temp_list = [1, 2, 3, 4, 5] # Create a list named temp_list containing the numbers 1 through 5
print(temp_list) # Print the entire list
temp_list = [1,2,3,4,5]
print(temp_list)
Result
[1,2,3,4,5]
The following snippet is a list that uses multiple data types
Example
print([1, {1}, (1, 2), “Hello”, 2.9]) # Print a list containing different data types
print([1,{1},(1,2),"Hello",2.9])
Result
[1, {1}, (1, 2), 'Hello', 2.9]
Indexing
Indexing means accessing any item inside the list by using its index. If you have a list named listOfstrings that contains ["a","b","c"], then listOfstrings[0] represents the first item. So, listOfstrings[0] is equal to a, listOfstrings[1] is equal to b, and listOfstrings[2] is equal to c.
Example
listOfstrings = [“a”, “b”, “c”] # Create a list named listOfstrings containing three letters
print(listOfstrings[0]) # Print the first element of the list (a)
print(listOfstrings[1]) # Print the second element of the list (b)
print(listOfstrings[2]) # Print the third element of the list (c)
listOfstrings = ["a","b","c"]
print(listOfstrings[0])
print(listOfstrings[1])
print(listOfstrings[2])
Result
a
b
c
You can use a smart index to access different elements inside lists, [-1] will return the last item inside the list
Example
temp_list = [“a”,”b”,”c”] # Create a list named temp_list containing three letters
print(temp_list[-1]) # Print the last item
listOfstrings = ["a","b","c"]
print(listOfstrings[-1])
Result
c
Or, you can use [-2] will return the second-to-last element of the list
Example
temp_list = [“a”,”b”,”c”] # Create a list named temp_list containing three letters
print(temp_list[-2]) # Print the second-to-last element
listOfstrings = ["a","b","c"]
print(listOfstrings[-2])
Result
b
Modify an item inside a list
You can modify any item inside lists because they are mutable.
Example
listOfitems = [“a”, “b”, “c”] # Create a list named listOfitems with three elements
listOfitems[0] = “aa” # Change the first element from “a” to “aa”
listOfitems[1] = 2022 # Change the second element from “b” to 2022 (integer)
print(listOfitems) # Print the updated list
listOfitems = ["a","b","c"]
listOfitems[0] = "aa"
listOfitems[1] = 2022
print(listOfitems)
Result
['aa', 2022, 'c']
Duplicates
A list can have duplicates, whereas a set cannot have duplicates
Example
listOfitems = [1, 2, 3] # Create a list named listOfitems with elements 1, 2, 3
listOfitems[1] = 1 # Change the second element (index 1) from 2 to 1
listOfitems[2] = 1 # Change the third element (index 2) from 3 to 1
print(listOfitems) # Print the updated list → Output: [1, 1, 1]
listOfitems = [1,2,3]
listOfitems[1] = 1
listOfitems[2] = 1
print(listOfitems)
Result
[1, 1, 1]
Loop through a list
You can loop through a list in a few ways, and you can use the for statement (Remember to indent after the for statement)
Example
temp_items = [1, 2, 3] # Create a list named temp_items with elements 1, 2, 3
for item in temp_items: # Loop through each element in the list
print(item) # Print the current element (item) in each iteration
temp_items = [1,2,3]
for item in temp_items:
print(item)
Result
1
2
3
Or, if you do not want to indent, you can do
Example
temp_items = [1, 2, 3] # Create a list named temp_items with elements 1, 2, 3
for item in temp_items:print(item): # Loop through each element in the list, print the current element (item) in each iteration
temp_items = [1,2,3]
for item in temp_items:print(item)
Result
1
2
3
Length
To get the length of a list, you can use the len function, or you can look through the items and increase a counter value
Example
temp_items = [1, 2, 3] # Create a list named temp_items with elements 1, 2, 3
print(len(temp_items)) # Print the size of the list
temp_items = [1,2,3]
print(len(temp_items))
Result
3
Add item
To add an item to a list, you can use .append method
temp_items = [1, 2, 3] # Create a list named temp_items with elements 1, 2, 3
temp_items.append(4) # Add the element 4 to the end of the list using append()
print(temp_items) # Print the updated list
Example
temp_items = [1,2,3]
temp_items.append(4)
print(temp_items)
Result
[1, 2, 3, 4]
Remove item by Value
To remove an item inside a list, you can use .remove method. This method will remove an item by value
Example
temp_items = [1, 2, 3] # Create a list named temp_items with elements 1, 2, 3
temp_items.remove(2) # Remove number 2 from the list
print(temp_items) # Print the updated list
temp_items = [1,2,3]
temp_items.remove(2)
print(temp_items)
Result
[1, 3]
Remove item by Index
To remove an item inside a list, you can use del statement. This statement will remove an item by index, but you will need to use [index]
Example
temp_items = [1, 2, 3] # Create a list named temp_items with elements 1, 2, 3
del temp_items[1] # Remove number 2 from the list by index
print(temp_items) # Print the updated list
temp_items = [1,2,3]
del temp_items[1]
print(temp_items)
Result
[1, 3]
Clear a list
To remove all items from a list, you can use the .clear method
Example
temp_items = [1, 2, 3] # Create a list named temp_items with elements 1, 2, 3
temp_items.clear() # Clear all the items from the list
print(temp_items) # Print the updated list
temp_items = [1,2,3]
temp_items.clear()
print(temp_items)
Result
[]
Pass By Reference
List is an immutable objects are passed by reference to function
Example
def change_value(param_in): # Define a function that takes one parameter called param_in
param_in.append(99) # Append the number 99 to the list param_in (modifies the original list)
var = [0, 1, 2, 3, 4, 5] # Create a list variable var with initial values
print(“Value before passing: “, var) # Print the list before calling the function
change_value(var) # Call the function and pass var; the list is modified inside the function
print(“Value after passing: “, var) # Print the list after the function call; shows the updated list
def change_value(param_in):
param_in.append(99)
var = [0,1,2,3,4,5]
print("Value before passing: ", var)
change_value(var)
print("Value after passing: ", var)
Result
Value before passing: [0, 1, 2, 3, 4, 5]
Value after passing: [0, 1, 2, 3, 4, 5, 99]