Course Content

Python Data Structures

## Python Data Structures

# Accessing 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.

`set_1 = {"green", "red", "white", "blue", "black"} for i in set_1: print(i)`

`set_ = {"green", "red", "white", "blue", "black"} print("red" in set_) print("yellow" in set_)`

Task

Here's your set:

You need to determine if `"pear"`

, `"banana"`

, and `"lemon"`

are in the set.

Task

Here's your set:

You need to determine if `"pear"`

, `"banana"`

, and `"lemon"`

are in the set.

Everything was clear?

# Accessing 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.

`set_1 = {"green", "red", "white", "blue", "black"} for i in set_1: print(i)`

`set_ = {"green", "red", "white", "blue", "black"} print("red" in set_) print("yellow" in set_)`

Task

Here's your set:

You need to determine if `"pear"`

, `"banana"`

, and `"lemon"`

are in the set.

Task

Here's your set:

You need to determine if `"pear"`

, `"banana"`

, and `"lemon"`

are in the set.

Everything was clear?

# Accessing 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.

`set_1 = {"green", "red", "white", "blue", "black"} for i in set_1: print(i)`

`set_ = {"green", "red", "white", "blue", "black"} print("red" in set_) print("yellow" in set_)`

Task

Here's your set:

You need to determine if `"pear"`

, `"banana"`

, and `"lemon"`

are in the set.

Task

Here's your set:

You need to determine if `"pear"`

, `"banana"`

, and `"lemon"`

are in the set.

Everything was clear?

`in`

keyword.

It's especially handy to use the `in`

keyword with a **for loop**. Let's check out an example.

`set_1 = {"green", "red", "white", "blue", "black"} for i in set_1: print(i)`

`set_ = {"green", "red", "white", "blue", "black"} print("red" in set_) print("yellow" in set_)`

Task

Here's your set:

You need to determine if `"pear"`

, `"banana"`

, and `"lemon"`

are in the set.