Python Sets


Set

A set is a collection which is unordered and unindexed. In Python sets are written with curly brackets.

Example

Create a Set:

thisset = {"apple", "banana", "cherry"}

print(thisset)

Note: Sets are unordered, so you cannot be sure in which order the items will appear.


Access Items

You cannot access items in a set by referring to an index, since sets are unordered the items has no index.

But you can loop through the set items using a for loop, or ask if a specified value is present in a set, by using the in keyword.

Example

Loop through the set, and print the values:

thisset = {"apple", "banana", "cherry"}


for x in thisset:

 print(x)

Example

Check if "banana" is present in the set:

thisset = {"apple", "banana", "cherry"}


print("banana" in thisset)

Jay Kakadiya

Jay Kakadiya Creator

I am a computer field, & i am Web developer.

Suggested Creators

Jay Kakadiya