Iterate through the Items of a Set in Python (FOR Loops) - Python Tutorial for Beginners

preview_player
Показать описание
🎓 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:

#Python #Tutorial #Beginners #Shorts

***

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

***

♡ FOLLOW US ♡

♡ SUPPORT US ♡

***
Рекомендации по теме
Комментарии
Автор

How to Iterate through Set items in Python, with FOR Loops? 🤔

DigitalAcademyOnline
Автор

Your channel is one of the BEST in your niche 👌🏻

chloejbt
welcome to shbcf.ru