Iterate through Items of a List in Python (FOR Loops)

🎓 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:
○ Data Types in Python – https://youtu.be/cweUByxBWiU
○ Operators in Python – https://youtu.be/-wDaVLkKOiU
○ IF Statements in Python – https://youtu.be/CC5seZ6OBJ4
○ FOR Loops in Python – https://youtu.be/JgH-D5DSTho

☞ WATCH MORE:
○ HOW TO Learn Python? Python Tutorial for Beginners [FULL Course] https://youtu.be/9hvnSZPMtuw

📖 Blog: http://digital.academy.free.fr/blog/

📖 Complete Python Development Course for Beginners [PLAYLIST]: http://digital.academy.free.fr/playlist/python-development-for-beginners

🛒 Shopping and Discounts: http://digital.academy.free.fr/store/

#Python #Tutorial #Beginners #Shorts

***

♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.

***

♡ FOLLOW US ♡
http://digital.academy.free.fr/
https://twitter.com/DigitalAcademyy
https://www.facebook.com/digitalacademyfr
https://www.instagram.com/digital_academy_fr/
https://www.youtube.com/c/DigitalAcademyOnline

♡ SUPPORT US ♡
http://digital.academy.free.fr/join
http://digital.academy.free.fr/donate
http://digital.academy.free.fr/subscribe
https://www.patreon.com/digital_academy
https://www.buymeacoffee.com/digital_academy

***

Copy link
Powered by Social Snap