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

Because sets are unordered, you cannot access their elements by index like you would with a list or tuple. However, you can:

  • Check for the existence of an element using the in keyword;
  • Iterate through the elements using a for loop.

Verifying Membership with in

The in keyword allows you to check if a specific element exists within a set:

123456
# Define a set of favorite movies movies = {"Inception", "Interstellar", "Tenet", "Dunkirk", "Memento"} # Check if specific movies are in the set print("Inception" in movies) # True print("Avatar" in movies) # False
copy

Iterating Through a Set with a for Loop

You can iterate over a set using a for loop to process each element individually. Since sets are unordered, the iteration order is unpredictable.

1234567
# Define a set of favorite movies movies = {"Inception", "Interstellar", "Tenet", "Dunkirk", "Memento"} # Iterate through the set and print each movie title print("Movie collection:") for movie in movies: print(movie)
copy

Each element in the set is accessed once during iteration. The order of elements in the output may vary.

Task
test

Swipe to show code editor

You will be working with a set of movies. First, use the in keyword to check if "Inception" is part of the collection. Print an appropriate message based on the result. Then, use a for loop to display all the movies 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

Because sets are unordered, you cannot access their elements by index like you would with a list or tuple. However, you can:

  • Check for the existence of an element using the in keyword;
  • Iterate through the elements using a for loop.

Verifying Membership with in

The in keyword allows you to check if a specific element exists within a set:

123456
# Define a set of favorite movies movies = {"Inception", "Interstellar", "Tenet", "Dunkirk", "Memento"} # Check if specific movies are in the set print("Inception" in movies) # True print("Avatar" in movies) # False
copy

Iterating Through a Set with a for Loop

You can iterate over a set using a for loop to process each element individually. Since sets are unordered, the iteration order is unpredictable.

1234567
# Define a set of favorite movies movies = {"Inception", "Interstellar", "Tenet", "Dunkirk", "Memento"} # Iterate through the set and print each movie title print("Movie collection:") for movie in movies: print(movie)
copy

Each element in the set is accessed once during iteration. The order of elements in the output may vary.

Task
test

Swipe to show code editor

You will be working with a set of movies. First, use the in keyword to check if "Inception" is part of the collection. Print an appropriate message based on the result. Then, use a for loop to display all the movies 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

Because sets are unordered, you cannot access their elements by index like you would with a list or tuple. However, you can:

  • Check for the existence of an element using the in keyword;
  • Iterate through the elements using a for loop.

Verifying Membership with in

The in keyword allows you to check if a specific element exists within a set:

123456
# Define a set of favorite movies movies = {"Inception", "Interstellar", "Tenet", "Dunkirk", "Memento"} # Check if specific movies are in the set print("Inception" in movies) # True print("Avatar" in movies) # False
copy

Iterating Through a Set with a for Loop

You can iterate over a set using a for loop to process each element individually. Since sets are unordered, the iteration order is unpredictable.

1234567
# Define a set of favorite movies movies = {"Inception", "Interstellar", "Tenet", "Dunkirk", "Memento"} # Iterate through the set and print each movie title print("Movie collection:") for movie in movies: print(movie)
copy

Each element in the set is accessed once during iteration. The order of elements in the output may vary.

Task
test

Swipe to show code editor

You will be working with a set of movies. First, use the in keyword to check if "Inception" is part of the collection. Print an appropriate message based on the result. Then, use a for loop to display all the movies 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!

Because sets are unordered, you cannot access their elements by index like you would with a list or tuple. However, you can:

  • Check for the existence of an element using the in keyword;
  • Iterate through the elements using a for loop.

Verifying Membership with in

The in keyword allows you to check if a specific element exists within a set:

123456
# Define a set of favorite movies movies = {"Inception", "Interstellar", "Tenet", "Dunkirk", "Memento"} # Check if specific movies are in the set print("Inception" in movies) # True print("Avatar" in movies) # False
copy

Iterating Through a Set with a for Loop

You can iterate over a set using a for loop to process each element individually. Since sets are unordered, the iteration order is unpredictable.

1234567
# Define a set of favorite movies movies = {"Inception", "Interstellar", "Tenet", "Dunkirk", "Memento"} # Iterate through the set and print each movie title print("Movie collection:") for movie in movies: print(movie)
copy

Each element in the set is accessed once during iteration. The order of elements in the output may vary.

Task
test

Swipe to show code editor

You will be working with a set of movies. First, use the in keyword to check if "Inception" is part of the collection. Print an appropriate message based on the result. Then, use a for loop to display all the movies 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
We're sorry to hear that something went wrong. What happened?
some-alt