Sitemap

[Python] Tuple & Set

4 min readAug 27, 2024

--

Tuple

In programming languages, a variable type known as a Tuple plays a role similar to that of a List. Both Tuples and Lists can hold various types of data, including integers, strings, other lists, floats, and more. However, there are key differences between them in terms of mutability and usage.

Tuples, like Lists, can store multiple items and can be used to group related data together. The major distinction lies in the immutability of Tuples. Once a Tuple is created, its contents cannot be altered, removed, or modified in any way. This means that you can add items to a Tuple only when it is initially created, but you cannot change, delete, or update the elements after that. This immutability makes Tuples ideal for storing data that should remain constant and unchanged throughout the execution of a program.

In contrast, Lists are mutable, meaning you can modify, add, or delete elements at any time. This flexibility allows Lists to be more versatile for scenarios where the data needs to be updated or changed frequently.

Overall, Tuples are typically used when you need a fixed collection of items that should not be modified, while Lists are used when you require a more dynamic and modifiable data structure.

x = ('jakarta','bekasi',1,2,3)
print(x.index('bekasi')) # 1
# will display the index of the value within the tuple

Set

In Python programming, there is also a variable type known as a Set. A Set is used to store collections of unique items, meaning that it automatically ensures that no duplicate values are present. This property makes Sets particularly useful for scenarios where you need to maintain a collection of distinct elements.

One key characteristic of Sets is that they do not allow duplicate values. If you attempt to add an element that already exists in the Set, the Set will not include it, thus preserving the uniqueness of its elements. This makes Sets an efficient choice for operations where uniqueness is required, such as removing duplicates from a list.

Another important feature of Sets is that they are unordered collections. When you print a Set, its elements are automatically sorted in ascending order. This sorting is a result of how Sets are implemented in Python, although it does not imply that Sets themselves maintain any inherent order. The output you see when printing a Set reflects a sorted view, but this is not a reliable way to retrieve elements in a specific sequence, as Sets do not guarantee order preservation.

Get Andhi YP’s stories in your inbox

Join Medium for free to get updates from this writer.

Overall, Sets are ideal for cases where you need to work with unique elements and do not require a specific ordering of those elements.

x = {1,5,'jakarta',5,6,'bye','bye'}
y = {55,22,22,22,31,36}

print(x) # {1, 'jakarta', 5, 6, 'bye'}
print(y) # {36, 55, 22, 31}

Berikut adalah beberapa contoh dari method yang dimiliki oleh Set.

add()

The methods provided by the Set type in Python include a function specifically designed for adding data to a Set variable. This function, known as the add() method, allows the programmer to insert new values into the Set. The values that can be inputted vary depending on the data the programmer needs to work with, and they can range from integers and strings to other data types.

However, the add() method comes with an important limitation: it only accepts a single element as its parameter at a time. If more than one value is passed to the add() method, an error will occur, as the method is not designed to handle multiple values simultaneously.

For example, if you attempt to input a list of multiple items, Python will raise a TypeError, since add() expects exactly one argument. This method is ideal for gradually building up a Set by adding elements one at a time, ensuring that each new addition is unique and does not violate the rules of the Set (i.e., no duplicates are allowed).

To add multiple values to a Set in one go, you would need to use a different method, such as update(), which can take an iterable like a list or another Set. However, for single-element additions, add() provides a straightforward way to incorporate new data into a Set while maintaining its uniqueness.

x.add('bogor','merak')
print(x) # will be error because only one data can added to Set in one time

x.add('bogor')
print(x) # {1, 5, 'bye', 6, 'jakarta', 'bogor'}

update()

The methods provided by the Set type in Python serve various functions, one of which is converting data into a Set. This is often used to combine two Sets into a single Set. During this merging process, any duplicate values are automatically eliminated, ensuring that the resulting Set contains only unique elements.

This feature is particularly useful when working with large datasets where duplicate entries need to be removed efficiently. By using a Set, you can merge multiple collections of data into one, while ensuring that no duplicate values remain. The end result is a clean Set containing only the distinct, non-repeating data, making Sets a powerful tool for managing collections of unique items in Python.

x = {5,6,7,8}
y = {7,8,9,20}
x.update(y)
print(x) # {5, 6, 7, 8, 9, 20}

--

--