Difference Between Set vs List vs Tuple


The utilization of data structures in Python presents a perplexing and convoluted method of depicting numbers, strings, and other Python objects in a collection of values. Python's built-in data structures, such as lists, tuples, and sets, are captivating and exhibit distinctive traits that differentiate them from one another. These data structures possess an extraordinary capacity to hold groups of objects, rendering them one of a kind. In this article, we will see which among the three fits best with examples.

List

Items can be changed, added to, or taken out of after they are made. It can also be altered and arranged in any order, and because it is categorized, individual items can be accessed based on where they are.

Example

my_list = [1, 2, 3, 4, 5]
print(my_list)

Output

[1, 2, 3, 4, 5]

A collection of items that can be changed and arranged is what you get when you make a list. The items can be enclosed within square brackets and the list() function used to create it. Indexing or slicing are used to access elements; the append() and extend() methods are used to add elements; the remove() and pop() methods are used to remove elements; the in keyword is used to check for elements; the len() function is used to determine the length of a list; and the sort() method is used to sort and reverse the order of the elements in the list.

Example

# Create a list
fruits = ['apple', 'banana', 'orange']

# Access elements
print(fruits[0])

# Add elements
fruits.append('kiwi')
print(fruits)

# Remove elements
fruits.remove('banana')
print(fruits)

# Check if element is in list
print('apple' in fruits)

# Find length of list
print(len(fruits))

# Sort the list
fruits.sort()
print(fruits)

# Reverse the list
fruits.reverse()
print(fruits)

Output

apple
['apple', 'banana', 'orange', 'kiwi']
['apple', 'orange', 'kiwi']
True
3
['apple', 'kiwi', 'orange']
['orange', 'kiwi', 'apple']

Tuple

a collection of things that cannot be changed, are ordered, and cannot be added to after they have been created; Also, you can't change anything about it); and indexable (you can locate a particular item by its location).

Example

my_tuple = (1, 2, 3, 4, 5)
print(my_tuple)

Output

(1, 2, 3, 4, 5)

One of two methods can be used to create them: either by using the tuple() function or by bracketing the components. Ordering or slicing are used to get at the parts of a tuple. Because they are permanent, no components can be changed after they are created. You can use the word "in'' to determine whether a tuple contains an ingredient. Utilize the len() function to determine a tuple's extent. Dates and locations, for example, are commonly stored in triples because they cannot be altered after they are formed.

Set

Unsorted gathering of distinct things that cannot contain duplicates; in addition, since sets are not indexed, individual items cannot be found based on their location.

Example

my_set = {1, 2, 3, 4, 5}
print(my_set)

Output

{1, 2, 3, 4, 5}

A comma-separated list of values enclosed in curly braces can be used to make a set in Python, which is an unordered collection of distinct components. The add() technique or update() method can be used to add items to a set. While the discard() method does nothing when removing components, the delete() method will generate an error. You can execute set procedures like union, intersection, and difference, and you can turn a list into a set using the set() function. When eliminating copies from a list or applying set procedures to it, this is helpful.

Example

# Create a set
numbers = {1, 2, 3, 4}

# Add elements
numbers.add(5)
print(numbers)

# Remove elements
numbers.discard(3)
print(numbers)

# Check if element is in set
print(2 in numbers)

# Find length of set
print(len(numbers))

# Perform set operations
set1 = {1, 2, 3}
set2 = {2, 3, 4}
print(set1.union(set2)) 
print(set1.intersection(set2)) 
print(set1.difference(set2))

# Convert list to set
numbers_list = [1, 2, 3, 4, 5]
numbers_set = set(numbers_list)

Output

{1, 2, 3, 4, 5}
{1, 2, 4, 5}
True
4
{1, 2, 3, 4}
{2, 3}
{1}

List vs. Tuple vs. Set - Comparison

After learning about the basics of list, tuple, and set data structures, let's compare them in terms of different aspects.

Mutable vs Immutable

Tuples are immutable, so when they are described, their component components cannot be altered. You can add, delete, or modify the components in records and sets because they are changeable. Use a tuple if you require a data format that cannot be modified; otherwise, use a set or list.

Order

Lists and tuples maintain the order of elements, whereas sets do not. Sets are unordered, and elements are arranged randomly.

Duplicate Elements

Lists and sets allow duplicate elements, whereas tuples do not. Sets have a unique property that they only store unique elements. Therefore, if you want to store only unique values, use a set, otherwise use a list or a tuple.

Performance

Due to the internal implementation of these data structures, sets are faster for membership checking and de-duplication, whereas lists and tuples are slower for indexing.

Applications

  • Python's most common data format is lists. They are useful for showing numbers in a rundown, like sections in a data set or lines in a record.

  • The elements of a vector and the coordinates of a point are two examples of fixed groups of linked values that are able to be expressed using tuples.

  • Sets are useful when you need to hold a variety of novel characteristics or perform specific tasks like association, convergence, and distinction.

Conclusion

We talked about the Python differences between lists, tuples, and sets in this tutorial. Records and sets are variable, meaning you can add, eliminate or alter the components in them, though tuples are unchanging, meaning you can't add, eliminate or adjust the components in them whenever they are characterized. Sets do not keep the order of the elements, but lists and tuples do. Lists and tuples can contain duplicate elements, whereas sets only allow for unique elements. Last but not least, indexing and membership checking take longer with sets than with lists and tuples. Contingent upon your requirements, you can pick the proper information design to make your code more successful and adaptable.

Updated on: 18-Apr-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements