Differences between List, Tuple, Set, and Dictionary in Python



Python’s data structures give us a mechanism to organize data in a way that makes it easy to access and modify. Collections are among the data structures in these. Lists, dictionaries, sets, and tuples are just a few examples of built-in collections that can be used to store data in Python. Python’s built-in data structures can be broadly categorized as either mutable or immutable. The term “mutable data structures” refers to ones that can have their elements added, removed, or changed. Lists, dictionaries, and sets are Python’s three mutable data structures. On the other side, immutable data structures cannot be changed after they have been created. In Python, a tuple is the only fundamentally built-in immutable data structure.

What is a List in Python

A list is a grouping of objects that might be of the same data type or multiple data types in Python. A list’s items are contained in square brackets and are separated by commas. Lists function similarly to dynamically scaled arrays that are defined in other languages (such as Java’s ArrayList and C++’s vector). The most effective tool in Python is the list because they don’t necessarily have to be homogeneous. One of the most popularly used data structures offered by Python is lists, which are collections of iterable, mutable, and ordered data. They might have duplicate data. Integer indices can be used to access the various elements of a list, with 0 serving as the index for the first element.

Lists come particularly handy when we need to store a variety of data types and then add, remove, or manipulate each piece individually. Lists can also be used to hold other data structures, including other lists, by building collections like lists of dictionaries, tuples, or other lists.

Various ways to create a list

Implementation of Python Code:

# Use square brackets to create a list that is empty.

list1 = []

# Use square brackets to create a four-item list.

list2 = [1, 5, "2", 6] # Keep in mind that this list contains two distinct data types: strings and numbers.

# Using the list() function, create an empty list.

list3 = list()

# Using the list() function, create a three-element list from a tuple.

list4 = list((4, 9, 1))

# Print out lists

print("List 1: ",list1)

print("List 2: ",list2)

print("List 3: ",list3)

print("List 4: ",list4)



List 1: []

List 2: [1, 5, '2', 6]

List 3: []

List 4: [4, 9, 1]

Try it out in an online compiler.

Applications of List in Python

  1. JSON format makes use of lists.
  2. Databases make use of lists.
  3. Lists are useful for array operations.

What is a Tuple in Python

Tuples are collections of different Python objects that are separated by commas. A tuple is similar to a list in some ways, such as indexing, nested objects, and repetition. The difference is that, unlike a list, a tuple is immutable. Tuples would be used if we required a data structure that, once formed, could not be changed again. If all of the components are immutable, tuples can also be utilized as dictionary keys.

Various ways to create a tuple

Implementation of Python code:

# Make a tuple with round brackets.

tuple1 = (1, 5, 2, 6)

# The tuple() function can be used to create a tuple from a list.

tuple2 = tuple([1, 5, 3, 4, 0])


# produces a tuple that is empty.



# Using the tuple() function, create a tuple.

tuple4 = tuple((4, 9, 0, 4, 9, 1))

# Display all the tuples.

print("Tuple 1: ",tuple1)

print("Tuple 2: ",tuple2)

print("Tuple 3: ",tuple3)

print("Tuple 4: ",tuple4)



Tuple 1: (1, 5, 2, 6)

Tuple 2: (1, 5, 3, 4, 0)

Tuple 3: ()

Tuple 4: (4, 9, 0, 4, 9, 1)


Applications of Tuple in Python

  1. Used to run one SQL query at a time to enter records into the database.
  2. Used for checking of parenthesis

What is a Set in Python

A Set is a data type for an unordered, iterable, and dynamic collection of items. The set class in Python is a representation of the mathematical concept of a set. It is not immutable, though, unlike a tuple. In Python, sets are characterized as mutable dynamic groups of immutable singular items. An immutable set must have immutable items. Although sets and lists may, at first glance, appear to be very similar, they differ greatly. When determining if a specific element is a member of a set, they are noticeably faster than lists. Sets are, by nature, unordered. They are not the greatest option if maintaining the insertion sequence is important to us.

How to create a set

Implementation of python code:

# Use curly brackets to create a set.

set1 = {3, 6, 2, 4}


# Using the set() function, create a set.

set3 = set([3, 4, 2, 5])


# display all the sets

print("Set 1: ",set1)

print("Set 2: ",set2)

print("Set 3: ",set3)


Set 1: {2, 3, 4, 6}

Set 2: {(8, 3, 'singla', 7), (1, 9, 'shivam', 2)}

Set 3: {2, 3, 4, 5}

Applications of Set in python

  1. To discover distinct or unique elements
  2. Join Operations


What is a Dictionary in Python

Python dictionaries are extremely comparable to dictionaries in the real world. These are modifiable data structures that have a set of keys and the associated values for those keys. They resemble word-definition dictionaries greatly thanks to their structure. Dictionary is a data type in Python that, unlike other data types that only carry a single value as an element, holds the key: value pairs. A dictionary is an ordered (as of Py 3.7) or unordered (as of Py 3.6 & earlier) collection of data values used to store data values like a map. An ordered set of data is called a tuThe dictionary contains key-value pairs to boost its effectiveness.ple. For easy access to specific information linked to a specific key, dictionaries are utilized. Uniqueness is crucial since we need to only access certain information and avoid mixing it up with other entries.

How to create a dictionary

Implementation of Python Code:

# Use curly brackets to make a blank dictionary.

dictionary1 = {}

# Use curly brackets to construct a three-element dictionary.

dictionary2 = {"Shivam": {"Age": 22, "Place": "Delhi"}, "Yash": {"Age": 21, "Place": "New Delhi"}}

# Because its values are other dictionaries, take note that the dictionary above has a more complicated structure!

# Use the dict() function to create a blank dictionary.

dictionary3 = dict()

# Using the dict() function, produce a three-element dictionary.

dictionary4 = dict([["three", 3], ["four", 4]])

# A list of lists was used to generate the dictionary, as you should be aware.


# display all the dictionaries

print("Dictionary 1: ",dictionary1)

print("Dictionary 2: ",dictionary2)

print("Dictionary 3: ",dictionary3)

print("Dictionary 4: ",dictionary4)


Dictionary 1: {}

Dictionary 2: {'Shivam': {'Age': 22, 'Place': 'Delhi'}, 'Yash': {'Age': 21, 'Place': 'New Delhi'}}

Dictionary 3: {}

Dictionary 4: {'three': 3, 'four': 4}


Applications of Dictionary in python

  1. Data frame with lists can be created using this.
  2. This can be used using JSON.


Difference Between List, Tuple, Set, and Dictionary in Python

Features Lists Tuples Sets Dictionaries
Indexing Yes Yes No Yes
Mutable Yes No Yes Yes(for values) and No(for keys)
Duplication of Data Yes Yes No No(for keys)
Ordered Yes Yes No Yes
Non homogenous Data Structure Yes Yes Yes Yes
Nested Among All Yes Yes Yes Yes
Representation The representation for list is [] The representation for tuple is () The representation for sets is {} The representation for dictionary is {}
Constructor function list() tuple() set() dict()
Creation of empty object

Making a list that is empty



Making a tuple that is empty


Making a set that is empty


Making a dictionary that is empty


Examples Example: [1,5, 2, 6] Example: (1,5, 2, 6) Example: {1,5, 2, 6} Example: {1: “s”, 2: “h”, 3: “i”, 4: “v”, 5: “a”,6: “m”}
Adding Element A new element is added to the end of the list using the append() method. The tuple cannot have an addition of an element. An element can be added to the set using add() method. If the key is absent, the update() method creates a new key-value pair and adds it to the dictionary. If the key does exist, it will, however, change the supplied key’s value to reflect the new value.
Remove element The item at the specified index is returned and removed from the list by the pop() function. Elements cannot be removed from the tuple. A random item will be returned and removed from the set by the pop() method. The supplied key value pair is removed from the dictionary via the pop() method, which also returns the value.
Sorting A list’s elements can be arranged in a specific ascending or descending order using the sort() method. Tuples are ordered and hence the elements cannot be rearranged. Since the set’s elements are unordered, they cannot be sorted. The keys in the dictionary are by default sorted using the sorted() method.
Reversing To reverse the list, use the reverse() method. For a tuple, no such technique is specified. For a set, no such technique is specified. The items cannot be reversed because they take the form of key-value pairs.
Use/Application In database and JSON format, lists are used. When entering records using a SQL query, tuples are used. The search for distinct elements and joining operations are done with sets. The lists are combined into a data frame using a dictionary, and is also utilised in JSON.

FAQs on Difference between List, Tuple, Set and Dictionary

Q1) What distinguishes List, Tuple, Set, and Dictionary in Python fundamentally from one another?

A list is an ordered collection of data, which is the primary distinction between a list, tuple, set, and dictionary in Python. An ordered set of data is called a tuple. A set is an unorganized collection. A dictionary is a collection of unsorted data that contains data in key-value pairs.


Q2) What are the representational differences between List, Tuple, Set, and Dictionary in Python?

The representation of a list in Python differs from that of a tuple, set, dictionary, and set, with a list being represented by []. The tuple is shown by the symbol (). The symbol {} represents the set: The dictionary is symbolized by the {}

Q3) In Python, what distinguishes List, Tuple, Set, and Dictionary in terms of the mutable?

Lists are mutable or have the ability to be altered. The tuple is immutable, and changes cannot be made to it. The set is mutable, which allows for modification. There are no duplicate elements, though. The dictionary can be changed. Keys aren’t duplicated, though.


  1. There are four major data structures in Python, three of which are mutable that are dictionaries, sets and lists, and tuples; the fourth one is immutable.
  2. Sets can be used when we need to compare two sets of data because they allow us to execute operations like intersection and difference on them.
  3. Every time we need to connect a key to a value and rapidly retrieve some data by a key, just like in a real-world dictionary, we should make use of dictionaries.
  4. To store heterogeneous data, lists can be used.
  5. Although immutable, tuples are comparable to lists, and when we do not want to unintentionally change the data, tuples can be used.


The post Differences between List, Tuple, Set, and Dictionary in Python appeared first on Datafloq.

Source link


Please enter your comment!
Please enter your name here