List, Tuple, Set & Dictionary in Python

Sankha Subhra
6 min readSep 1, 2023

--

🔹Overview :

Python data structures allow us to store and organize data in a form that is easily accessible and customizable. Collections are among these data structures. Lists, sets, tuples, and dictionaries are some of the most often used built-in collections.

🔹Introduction :

Having trouble understanding what Python’s Lists, tuples, sets, and dictionaries are? Everyone who works with Python has been perplexed by the differences between them or when to utilize them. Say no more, since we’ll dissect and comprehend the differences between these Data structure collections so that we never forget!

Let’s look more closely at each of these collections:

≫ They Look Similar:

These four data types all hold multiple values. The only difference is the usage of special symbols to surround the values.

You can notice the difference. However, this does not make one data type superior to another.

So, what are the actual differences?

≫ Key Differences:

◉ Syntax:

  • List: Employs square brackets [ ] with comma-separated elements.
  • Tuple: Employs parentheses ( ) with comma-separated elements.
  • Set: Utilizes curly brackets { } with comma-separated elements.
  • Dictionary: Uses curly brackets { } with key-value pairs separated by commas.

◉ Mutability:

  • List: Mutable (modifiable).
  • Tuple: Immutable (non-modifiable).
  • Set: Mutable, but elements inside must be immutable.
  • Dictionary: Mutable; keys are immutable, but values can change.

◉ Order:

  • List: Maintains order of elements.
  • Tuple: Maintains order of elements.
  • Set: No guaranteed order.
  • Dictionary: Insertion order is preserved in Python 3.7+.

◉ Uniqueness:

  • List: Allows duplicates.
  • Tuple: Allows duplicates.
  • Set: Only unique elements.
  • Dictionary: Unique keys and values can be duplicated.

◉ Data Structure:

  • List: Ordered collection.
  • Tuple: Ordered collection.
  • Set: Unordered collection.
  • Dictionary: Collection of key-value pairs.

◉ Indexing:

  • List: Integer-based indexing starting from 0.
  • Tuple: Integer-based indexing starting from 0.
  • Set: No index-based mechanism.
  • Dictionary: Key-based indexing.

🔴Difference Between List Vs Tuple Vs Set Vs Dictionary in Python🔴

Let us know more about Lists, Tuples, Sets and Dictionaries in the following topics:

List:

Lists are one of the most commonly used data structures in Python; they are a collection of iterable, mutable, and ordered data. They may include redundant data.

Syntax

Code

list1 = [1 , 2, 'abc', 3, 'def']
list2 = []
list3 = list((1, 2, 3))
print(list1)
print(list2)
print(list3)

Output

Indexing

Code

list1 = [1, 2, "hello", 3]
print(list1[2])

Output

Adding New Element

Code

list1 = ["one", "two", "three"]
list1.append("four")
print(list1)

Output

Deleting Element

Code

list1 = ["one", "two", "three", "four"]
list1.pop()
print(list1)

Output

Sorting Elements

Code

list1 = [100, 131, 14, 1, 43]
list1.sort()
print(list1)

Output

Searching Elements

Code

list1 = [100, 131, 14, 1, 43]
# index() returns index for 4 in list1
print(list1.index(131))

Output

Reversing Elements

Code

list1 = [100, 131, 14, 1, 43]
list1.reverse()
print(list1)

Output

Counting Elements

Code

list1 = [101, 200, 333, 101, 846, 343, 101]
print(list1.count(101))

Output

Tuple:

Tuples are similar to lists. This collection also has iterable, ordered, and (can contain) repetitive data, just like lists. But unlike lists, tuples are immutable.

Syntax

Code

tuple1 = (1, 2, 'abc', 3, 4)
tuple2 = ()
tuple3 = tuple((1, 3, 5, "hello"))
print(tuple1)
print(tuple2)
print(tuple3)

Output

Indexing

Code

list1 = [1, 2, "hello", 3]
print(list1[2])

Output

Set:

Another type of data structure is a set, which is a collection of unordered, iterable, and mutable data. But it only contains unique elements.

Syntax

Code

set1 = {1, 2, 3, 'abc', 4, 8}
print(set1)

Output

Dictionary:

Dictionaries, unlike all other collection types, only hold key-value pairs.

Python version <3.7: is an unordered data collection.

Python v3.1 introduced a new type of dictionary called ‘OrderedDict,’ which was comparable to the dictionary in Python; the difference being that orderedDict was ordered (as the name implies).

In the most recent version of Python, 3.7: Finally, the dictionary is now an ordered collection of key-value pairs in Python 3.7. The order is now assured in the order in which they were placed, i.e. the order in which they were inserted.

Syntax

Code

dict1 = {"key1": "value1", "key2": "value2"}
dict2 = {}
dict3 = dict({1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'})
print(dict1)

print(dict2)

print(dict3)

Output

Indexing

Code

dict1 = dict({1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'})
print(dict1.keys())
print(dict1.values())
print(dict1[4])

Output

Adding New Element

Code

dict1 = {'India': 91, 'Russia': 7, 'Australia': 61}
dict1.update({'Canada': 1})
print(dict1)
dict1.pop('Australia')
print(dict1)

Output

Deleting Element

Code

dict1 = {'India': 91, 'Russia': 7, 'Australia': 61}
dict1.pop('Russia')
print(dict1)

Output

Sorting Elements

Code

dict1 = {'India': 91, 'Russia': 7, 'Australia': 61}
print(sorted(dict1))

Output

Searching Elements

Code

dict1 = {'India': 91, 'Russia': 7, 'Australia': 61}
print(dict1['Australia'])

Output

NOTE: An ordered collection is one in which the data structure keeps the order in which the data was added, whereas an unordered collection does not.

  • Sets are an unordered collection, whereas a dictionary (in Python 3.6 and before) may appear ordered but is not. It keeps the keys that have been added to it, but at the same time, accessibility at a specific integer index is not possible; instead, accessibility via the key is possible.
  • Each key in a dictionary is unique and acts as an index by allowing access to values. However, the order in which keys are stored in a dictionary is not maintained, therefore the dictionary is unordered. Whereas Dictionary in Python 3.7 and ‘OrderedDict’ in Python 3.1 are ordered collections of key-value data that maintain the insertion order.

🔹Conclusion :

  • Mutability differs between lists and tuples because lists are mutable while tuples are not.
  • Set is the one and only unordered collection in Python 3.7.
  • Dictionaries store data in the form of key-value pairs in Python and remain a controversy when it comes to whether they’re ordered or unordered. Because this differs between Python versions. Python v<=3.6 has dictionaries as unordered, but at the same time, orderedDict was introduced in Python 3.1, and then finally Python 3.7 has both orderedDict and Dictionary and now both are ordered.

Hope this information gives more clarity on what are the differences between list, tuple, set and dictionary in Python. And what use cases do they serve and hope it gives you clarity on when to use which collection.

--

--