LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.
Lesson 21: for Loops In Python
With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc. A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).In the context of most data science work, Python for loops are used to loop through an iterable object (like a list, tuple, set, etc.) and perform the same action for each entry. For example, a for loop would allow us to iterate through a list, performing the same action on each item in the list.
Lists: How to Loop Through a List?
You can loop through the list items by using a for loop.
Example: Print all items in the list, one by one.
output will be
apple
banana
cherry
grapes
How to Loop Through the Index Numbers?
Example; Print all items by referring to their index number.
output will be
apple
banana
cherry
grapes
Note; The iterable created in the example above is [0, 1, 2, 3].
Using a While Loop: You can also loop through the list items by using a while loop.
Use the len() function to determine the length of the list, then start at 0 and loop your way
through the list items by refering to their indexes. Remember to increase the index by 1 after each iteration.
Example: Print all items, using a while loop to go through all the index numbers.
output will be
apple
banana
cherry
grapes
How to Loop Through A List Using List Comprehension?
List Comprehension offers the shortest syntax for looping through lists.
Example: A short hand for loop that will print all items in a list.
listA = ["apple", "banana", "cherry", "grapes"]
[print(x) for x in listA]
output will be
apple
banana
cherry
grapes
More About List Comprehension
List comprehension offers a shorter syntax when you want to create a new list based on the
values of an existing list.
For instane based on a list of fruits, you want a new list, containing only the fruits with the
letter "a" in the name. Without list comprehension you will have to write a for statement with a
conditional test inside.
But with list comprehension you can do all that with only one line of code.
Example
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x for x in fruits if "a" in x]
print(newlist)
output will be
['apple', 'banana', 'mango']
Important to Remember
List items are ordered, changeable, and allow duplicate values.
List items are indexed, the first item has index [0], the second item has index [1] etc.
When we say that lists are ordered, it means that the items have a defined order, and that order
will not change. If you add new items to a list, the new items will be placed at the end of the list.
Changeable: The list is changeable, meaning that we can change, add, and remove items in a list after
it has been created.
Since lists are indexed, lists can have items with the same value i.e. lists Allow Duplicates.
Example
thislist = ["apple", "banana", "cherry", "apple", "cherry"]
print(thislist)
output will be
['apple', 'banana', 'cherry', 'apple', 'cherry']
Python Collections (Arrays): There are four collection data types in the Python programming language.
List is a collection which is ordered and changeable. Allows duplicate members.
Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
Set is a collection which is unordered, unchangeable*, and unindexed. No duplicate members.
Dictionary is a collection which is ordered** and changeable. No duplicate members.