Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Accessing the Elements of a Set | Set
Python Data Structures
course content

Course Content

Python Data Structures

Python Data Structures

1. List
2. Dictionary
3. Tuple
4. Set

bookAccessing the Elements of a Set

Since a set is an unordered collection, you can't access its elements by index because they don't have indices. However, you can verify whether an element exists within the set using the in keyword.

It's especially handy to use the in keyword with a for loop. Let's check out an example.

1234
set_1 = {"green", "red", "white", "blue", "black"} for i in set_1: print(i)
copy
1234
set_ = {"green", "red", "white", "blue", "black"} print("red" in set_) print("yellow" in set_)
copy

Task

Here's your set:

You need to determine if "pear", "banana", and "lemon" are in the set.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 4
toggle bottom row

bookAccessing the Elements of a Set

Since a set is an unordered collection, you can't access its elements by index because they don't have indices. However, you can verify whether an element exists within the set using the in keyword.

It's especially handy to use the in keyword with a for loop. Let's check out an example.

1234
set_1 = {"green", "red", "white", "blue", "black"} for i in set_1: print(i)
copy
1234
set_ = {"green", "red", "white", "blue", "black"} print("red" in set_) print("yellow" in set_)
copy

Task

Here's your set:

You need to determine if "pear", "banana", and "lemon" are in the set.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 4
toggle bottom row

bookAccessing the Elements of a Set

Since a set is an unordered collection, you can't access its elements by index because they don't have indices. However, you can verify whether an element exists within the set using the in keyword.

It's especially handy to use the in keyword with a for loop. Let's check out an example.

1234
set_1 = {"green", "red", "white", "blue", "black"} for i in set_1: print(i)
copy
1234
set_ = {"green", "red", "white", "blue", "black"} print("red" in set_) print("yellow" in set_)
copy

Task

Here's your set:

You need to determine if "pear", "banana", and "lemon" are in the set.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Since a set is an unordered collection, you can't access its elements by index because they don't have indices. However, you can verify whether an element exists within the set using the in keyword.

It's especially handy to use the in keyword with a for loop. Let's check out an example.

1234
set_1 = {"green", "red", "white", "blue", "black"} for i in set_1: print(i)
copy
1234
set_ = {"green", "red", "white", "blue", "black"} print("red" in set_) print("yellow" in set_)
copy

Task

Here's your set:

You need to determine if "pear", "banana", and "lemon" are in the set.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 4. Chapter 4
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt