Accessing Set Items in Python: Retrieving Elements from Sets

thumb_up 1  ·  sell Python set item access, Accessing elements in Python sets, Retrieving set items in Python

Since set is not a sequence data type, its items cannot be accessed individually as they do not have a positional index (as in list or tuple). Set items do not have a key either (as in dictionary) to access. You can only traverse the set items using a for loop.

Example 1

 
langs = {"C", "C++", "Java", "Python"} for lang in langs: print (lang)

It will produce the following output −

Python
C
C++
Java

Example 2

Python's membership operators let you check if a certain item is available in the set. Take a look at the following example −

 
langs = {"C", "C++", "Java", "Python"} print ("PHP" in langs) print ("Java" in langs)

It will produce the following output −

False
True



The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back