🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!
🖥️ Iterate through the Items of a Set in Python with FOR Loops
Sets are a very useful and a widely used data structures in Python. As a developer, you will often be in situations where you will need to iterate through a set, while you perform some actions on its items. Therefore, the most common way to iterate through a Set is using FOR Loops in Python.
my_set = {'red', 'green', 'blue'}
for my_item in my_set:
print(my_item)
# OUTPUT: red green blue
This works pretty well if you only need to get the items of the Set in Python. But, if you want to know at which position an item is, you will need the indexes. A common way to do that is to convert your set into a list, combine the functions range() and len() altogether, or use the enumerate() function in Python. If you do not know how the enumerate() function works in Python, please check out a suggested video about: Python FOR Loops.
○ Find the Length of a Set in Python (Size)
Eventually, you may want to find how many items a Set has. In that case, you do not have to iterate through the entire Set and count each item, but use the function len() – which returns the number of items in a Set in Python.
my_set = {'red', 'green', 'blue'}
print(len(my_set)) # 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
***