Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

sets
tuples
python
communitycreator

Sets vs. tuples in Python

Suma

Sets

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 = " ")
print(people)
  
# This will add Shinchan
# in the set
people.add("Shinchan")
print("\nSet after adding element:", end = " ")
print(people)

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)
  
print(population)

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)   
print(new_set)

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)
  
print(new_set)

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.

Tuples

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 );
print(tup1[2])
print(tup2[2:5])

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);
print(tup)
del tup
print("After deleting tup : ")
print(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.

RELATED TAGS

sets
tuples
python
communitycreator
RELATED COURSES

View all Courses

Keep Exploring