filmov
tv
Iterate through Items of a List in Python (FOR Loops) - Python Tutorial for Beginners
Показать описание
🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!
🖥️ Iterate through Items of a List in Python (FOR Loops)
Lists are a very useful and a widely used data structure, in Python. As a developer, you will often be in situations where you will need to iterate through a list, while you perform some actions on its items. So, the most common way to iterate through a list in Python, is with a FOR Loop.
my_list = ['red', 'green', 'blue']
for item in my_list:
print(item)
This works well if you only need to read the items of the List. But, if you want to update them, you need the indexes. A common way to do that, is to combine the range() and len() functions altogether. For instance, this loop iterates through the list and double each item. If you do not know how the range() function works, please check out a suggested video about: Python FOR Loops.
my_list = [1, 2, 3, 4]
for i in range(len(my_list)):
my_list[i] = my_list[i] * 2
# my_list = [2, 4, 6, 8]
---
○ Length of a List in Python
Eventually, you may want to find how many items a list has. In that case, you do not have to iterate through the entire list and count each item, but use the function len() - which returns the number of items in a list.
my_list = ['red', 'green', 'blue']
print(len(my_list)) # OUTPUT: 3
Let's play this video, stick around and watch until the end of this video! 👍🏻
- Digital Academy™ 🎓
***
☞ WATCH NEXT:
#Python #Tutorial #Beginners #Shorts
***
♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.
***
♡ FOLLOW US ♡
♡ SUPPORT US ♡
***
🖥️ Iterate through Items of a List in Python (FOR Loops)
Lists are a very useful and a widely used data structure, in Python. As a developer, you will often be in situations where you will need to iterate through a list, while you perform some actions on its items. So, the most common way to iterate through a list in Python, is with a FOR Loop.
my_list = ['red', 'green', 'blue']
for item in my_list:
print(item)
This works well if you only need to read the items of the List. But, if you want to update them, you need the indexes. A common way to do that, is to combine the range() and len() functions altogether. For instance, this loop iterates through the list and double each item. If you do not know how the range() function works, please check out a suggested video about: Python FOR Loops.
my_list = [1, 2, 3, 4]
for i in range(len(my_list)):
my_list[i] = my_list[i] * 2
# my_list = [2, 4, 6, 8]
---
○ Length of a List in Python
Eventually, you may want to find how many items a list has. In that case, you do not have to iterate through the entire list and count each item, but use the function len() - which returns the number of items in a list.
my_list = ['red', 'green', 'blue']
print(len(my_list)) # OUTPUT: 3
Let's play this video, stick around and watch until the end of this video! 👍🏻
- Digital Academy™ 🎓
***
☞ WATCH NEXT:
#Python #Tutorial #Beginners #Shorts
***
♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.
***
♡ FOLLOW US ♡
♡ SUPPORT US ♡
***
Комментарии