Python Tuples and Sets

In this tutorial you will learn:

  • Python Tuples
  • Python Sets
  • Difference between Lists, Tuples and Sets
  • Accessing elements in Tuples and Sets
  • Adding elements in Tuples and Sets
  • Updating elements in Tuples and Sets
  • Removing elements from Tuples and Sets

Python Tuples

Tuple is simply a collection of immutable Python objects it means that those objects cannot be changed. One of the properties of Tuples is that they are ordered. In order to make a Tuple in Python we use round brackets.

For example:

  1. <p>emptyTuple = () #declares an empty Tuple
  2. tupleOfNumbers = (7, 3 ,10) #declares a Tuple of numbers
  3. tupleOfStrings = (“Python”, “Test”) #Tuple of strings</p>

Python Sets

Sets in python is also a collection of objects but they are unordered. One of the major advantages of a Set is that it cannot contain multiple instances of the same element, which makes it very useful in scenarios when we don’t want repetition in our data. Sets are declared using curly brackets.

For example:

  1. emptySet = {} #declares an empty Set
  2. setOfNumbers = {4,1,6} #declares a Set of numbers
  3. setOfStrings = {"Python", "Test", "Programming", "Book", "Table"}#Set of strings
  4. print("Output Result")
  5. print(emptySet)
  6. print(setOfNumbers)
  7. print(setOfStrings)

Difference between Lists, Tuples and Sets

Lists are mutable while Sets and Tuples are immutable and hence unchangeable. Square brackets are used for Lists, round brackets are used for Tuples and curly brackets are used for Sets. Lists and Tuples can contain duplicate elements while Sets cannot contain duplicate elements, this is one of the major difference that differentiates Sets from these other data types. Lists and Tuples are ordered while Sets are unordered so we cannot say in which order the elements would appear in the result. This can also be seen in the result screenshot of the above code. Note the order of printed data it’s not the same as declared in the code.

Accessing elements in a Tuple

In order to access elements in a Tuple we use square brackets just we used to access elements in a List. We can either use the index directly. Or slice the Tuple to get a range of values.

For example:

  1. myTuple1 = (89, 33, "Python", "Test", "Programming")
  2. print("Output Result")
  3. print("Third element of tuple: ", myTuple1[2]) #directly through index
  4. print("Third to 5th element of tuple: ", myTuple1[2:5]) #slicing

Accessing elements in a Set

In order to access elements in a set we cannot use an index with square brackets since sets are unordered and their elements don’t have an index. To check if an element exist in a set we use ‘in’ keyword. Another way is to loop through the Set which will be discussed at a later stage.

For example:

  1. print("Set Output Result")
  2. print("Third element of set: ", "Python" in mySet1) #It will return a true or false if that element exists in set
  3. print("Complete set: ", mySet1)

Adding elements in Tuples

Once a Tuple is created we cannot add elements to it.

Adding elements in Sets

We can add single element in a set using the add() function. In order to add multiple elements Python also provides us with an update function which can add multiple values at once.

For example:

  1. print("\n Adding elements in sets usind add()")
  2. mySet1.add("Coding")
  3. mySet1.add("Skills")
  4. print(mySet1)
  5. print("\n Adding elements in sets using update()")
  6. mySet1.update(["tutorial", 1, 2 ,3 ])
  7. print(mySet1)

Updating elements in Tuples and Sets

As we discussed in the previous tutorial that Tuples and Sets are immutable so their values cannot be changed once they are assigned.

Removing elements from Tuples and Sets

Tuples are unchangeable so you cannot delete individual elements but you can delete the whole Tuple using del keyword. As for Sets elements can be removed using the remove function.

  1. print("\n Removing elements in sets using")
  2. del myTuple1
  3. mySet1.remove("Programming")
  4. mySet1.remove("Skills")
  5. print(mySet1)

Add new comment