Loop through a list

Often times you would want to run through all the items in a list and perform the same action on all items. The easiest way to loop through a list is to use the for loop:

names = ['Maria', 'Lucy', 'Angelina', 'Tanya']
new_name = 'Tanya'
if new_name in names:
    print('The name already exists. Please choose another one.')

The for name in names line instructs the Python to take the item from the names list and store it in the variable name. We’ve then printed the content of the name variable.

Geek University 2022