DEV Community

Sakthivel V
Sakthivel V

Posted on

Set , Tuples in Python

Python Tuples:

  • Tuples are used to store multiple items in a single variable.
  • A tuple is a collection which is ordered and unchangeable.
  • A tuple is like a list, but it cannot be changed (immutable).
  • Written inside round brackets ().

Ex:

tup = (5, 'Welcome', 7.5, True, [1, 2, 3], {'key': 'value'})
print(tup)
Enter fullscreen mode Exit fullscreen mode

Change Tuple Values:

  • Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called.

  • You can convert the tuple into a list, change the list, and convert the list back into a tuple.

Ex:

x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)

print(x)

Enter fullscreen mode Exit fullscreen mode

Join Two Tuples:

  • To join two or more tuples you can use the + operator

Ex:

tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)

tuple3 = tuple1 + tuple2
print(tuple3)
Enter fullscreen mode Exit fullscreen mode

Set:

  • Sets are used to store multiple items in a single variable.

  • A set is a collection which is unordered, unchangeable.

Ex:

thisset = {"apple", "banana", "cherry"}
print(thisset)
Enter fullscreen mode Exit fullscreen mode
  • Sets cannot have two items with the same value. Duplicate values will be ignored:

Ex:

thisset = {"apple", "banana", "cherry", "apple"}
print(thisset)
Enter fullscreen mode Exit fullscreen mode

Add Items and Remove Item:

  • To add one item to a set use the add() method.

  • To add items from another set into the current set, use the update() method.

Ex:

thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)

thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}
thisset.update(tropical)
print(thisset)
Enter fullscreen mode Exit fullscreen mode

Remove Item:

  • To remove an item in a set, use the remove(), or the discard() method.

  • You can also use the pop() method to remove an item, but this method will remove a random item, so you cannot be sure what item that gets removed.

  • The return value of the pop() method is the removed item.

Ex:

thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
print(thisset)


thisset = {"apple", "banana", "cherry"}
x = thisset.pop()
print(x)
print(thisset)
Enter fullscreen mode Exit fullscreen mode
  • The clear() method empties the set Ex:
thisset = {"apple", "banana", "cherry"}
thisset.clear()
print(thisset)
Enter fullscreen mode Exit fullscreen mode

Join Sets():

  • There are several ways to join two or more sets in Python.

  • The union() and update() methods joins all items from both sets.

  • The intersection() method keeps ONLY the duplicates.

  • The difference() method keeps the items from the first set that are not in the other set(s).

  • The symmetric_difference() method keeps all items EXCEPT the duplicates.

Union():

  • The union() method returns a new set with all items from both sets.

  • You can use the | operator instead of the union() method, and you will get the same result.

Ex:

set1 = {"a", "b", "c"}
set2 = {1, 2, 3}

set3 = set1 | set2
print(set3)
Enter fullscreen mode Exit fullscreen mode

Intersection():

  • The intersection() method will return a new set, that only contains the items that are present in both sets.

  • We can use the & operator instead of the intersection() method, and you will get the same result.

Ex:

set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}

set3 = set1 & set2
print(set3)
Enter fullscreen mode Exit fullscreen mode

Difference():

  • The difference() method will return a new set that will contain only the items from the first set that are not present in the other set.

  • You can use the - operator instead of the difference() method, and you will get the same result.

Ex:

set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}

set3 = set1 - set2
print(set3)
Enter fullscreen mode Exit fullscreen mode

Symmetric Differences():

  • The symmetric_difference() method will keep only the elements that are NOT present in both sets.

  • You can use the ^ operator instead of the symmetric_difference() method, and you will get the same result.

Ex:

set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}

set3 = set1 ^ set2
print(set3)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)