Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


Sets vs. tuples in Python



A Set is an unordered collection data type. It is iterable and mutable with no duplicate elements.

Sets are unordered, so we cannot access items with indexes the way we can in lists.

How to work with sets

How you work with sets is based on a data structure known as a hash table. The values stored in sets can be changed because, since they are unordered, they don’t have a fixed position.

Set methods

1. How to add elements

Insertion inset is done through set.add() function, where a certain value is created to store in the hash table.

# Creating a Set
people = {"Oswald", "Doraemon", "Noddy"}
print("People:", end = " ")
# This will add Shinchan
# in the set
print("\nSet after adding element:", end = " ")

2. Union

Two sets can be merged into one set using the union() function or | operator.

# Python Program to
# demonstrate union of
# two sets
people = {"Noddy", "Shin", "Dora"}
students = {"sam", "sama"}
# Union using union()
# function
population = people.union(students)

3. Intersection

This can be achieved through the intersection() or & operator, which are used to find out the common elements amongst two sets.

# Python program to 
# demonstrate intersection
# of two sets
set1 = {"shin", "dora", "cat"}
set2 = {"shin", "cat", "dog", "lion"}
new_set = set1.intersection(set2)   

4. Difference

To find differences in the elements in both sets. The elements that don’t exist in one set will be returned if we consider them with respect to the second set.

# Python program to 
# demonstrate difference
# of two sets
set1 = {2,4,6,8,10}
set2 = {4,6,8,12,14}
# Difference of two sets
# using difference() function
new_set = set1.difference(set2)

There are two main issues with Python sets:

  • The set doesn’t maintain elements in any particular order.

  • Only immutable types can be added to a Python set.


A tuple is a collection of objects that are ordered and immutable. Tuples use parentheses; whereas, lists use square brackets.

Tuple method

1. How to access values in a Tuple

There are various ways to access the tuples. Different methods are indexingboth positive and negative and slicing.

tup1 = ('sam', 'sam2', 'sam3', 113, 116);
tup2 = (1, 2, 3, 4, 5, 6, 7 );

2. How to update tuples

Tuples are immutable, which means you cannot update or change the values of tuple elements.

tup1 = (12, 25.378);
tup2 = ('abc', 'xyz');

# create a new tuple as follows
tup3 = tup1 + tup2
print (tup3)

3. How to delete tuple elements

It is not possible to remove tuple elements separately. In order to remove an entire tuple, you have to use the del statement.

tup = ('noddy', 'doraemon', 12, 24);
del tup
print("After deleting tup : ")

Built-in tuple methods

A) Index( ) - finds the tuple and returns the index of the given value where it is available.

B) Count( ) - returns the frequency of occurrence of a specified value.

Advantages of Tuples

A) Tuples have a better overall performance boost.

B) Tuples have elements that can be used as a key in a dictionary.

C) Tuples are used to store Values of different data types.

D) Tuples are used to format strings.

Basically, tuples are a sequence of objects, whereas sets are a collection of objects in any order. This means that tuples are, more or less, ordered sets.



View all Courses

Keep Exploring