Signup/Sign In
PUBLISHED ON: FEBRUARY 2, 2021

Traverse a Set using range method

In this article, we will learn to traverse or iterate over elements of a set in Python. We will traverse the set using the built-in range() method with the help of an example to better understand the topic. Let's first have a quick look over what is a set in Python.

Python Set

Python has a built-in data type called set. It is a collection of unordered data values. Due to the unordered dataset, the user is not sure of the order in which data values appear. An unordered dataset leads to unindexed values. Set values cannot be accessed using index numbers as we did in the list. Set values are immutable which means we cannot alter the values after their creation. Data inside the set can be of any type say, integer, string, or float value. The set uses comma-separated values within curly brackets {} to store data. Sets can be defined using any variable name and then assigning different values to the set in the curly bracket. The set is unordered, unchangeable, and does not allows duplicate values. For example,

set1 = {"Ram", "Arun", "Kiran"}
set2 = {16, 78, 32, 67}
set3 = {"apple", "mango", 16, "cherry", 3}

Traverse a Set using range() Method

Set is an unordered and unindexed data type, so the set does not allow accessing the elements using index values. All of the below examples convert the set to list data type before performing iteration process using range() function and it results in a new list of the set data type. The elements are printed in an unordered manner with no duplicate elements.

Example: Using indexed access by converting the set to list type

The below example traverses the given input set using the range() method by converting the set to the list. list() takes the input set as an argument and converts the set to the list data type. range() method is used to iterate over the elements of the new list starting from 0 index value to the length of the list (exclusive) which is calculated using len().

#input set
set1 = {'s','t','u','d','y','t','o','n','i','g','h','t'}

#convert set to list
list1 = list(set1)

# Traverse the set
for x in range(0,len(list1)):
    print(list1[x], end='')


ngsyitohdu

Example: Using negative index by converting the set to list type

The below example traverses the given input set using the range() method by converting the set to the list. list() takes the input set as an argument and converts the set to the list data type. range() method is used to iterate over the elements of the new list starting from the last index value to 0.

set1 = {'s','t','u','d','y','t','o','n','i','g','h','t'}

#convert set to list
list1 = list(set1)

# Traverse the set
for x in range(-len(list1),0):
    print(list1[x], end='')


snightydou

Example: Using slicing after converting the set to list type

The below example traverses the given input set using the range() method by converting the set to the list. list() takes the input set as an argument and converts the set to the list data type. range() method is used to iterate over the elements of the new list starting from 1 index value to the length of the list (exclusive) which is calculated using len(). It results in the list of each character as shown in the output.

set1 = {'s','t','u','d','y','t','o','n','i','g','h','t'}

#convert set to list
list1 = list(set1)

# Traverse the set
for x in range(1,len(list1)):
    print(list1[x-1:x], end='')
print(list1[-1:])


['o']['d']['g']['t']['i']['n']['h']['s']['u']['y']

Conclusion

In this article, we learned to traverse elements of a set by using range() method. We used the different examples of converting the set to list type and then iterating over the elements using for loop. We traversed the set using negative indexes and slicing operation too.



About the author:
An enthusiastic fresher, a patient person who loves to work in diverse fields. I am a creative person and always present the work with utmost perfection.