Difference between list, tuple and set in python

  1. List:- Lists are just like dynamic-sized arrays, declared in other languages (vector in C++ and ArrayList in Java). Lists need not be homogeneous always which makes it the most powerful tool in Python.

    The main characteristics of lists are –:

    i)The list is a datatype available in Python that can be written as a list of comma-separated values (items) between square brackets.

    ii) Lists are mutable i.e. they can be converted into another data type and can store any data element in it.

    iii) List can store any type of element.

for example:- list_demo = ['hello', 123, 'world', 1.546]

  1. Tuple:- Tuple is a collection of Python objects much like a list. The sequence of values stored in a tuple can be of any type, and they are indexed by integers. Values of a tuple are syntactically separated by ‘commas’. Although it is not necessary, it is more common to define a tuple by closing the sequence of values in parentheses. The main characteristics of tuples are –:

    i)Tuple is an immutable sequence in python.

    ii)It cannot be changed or replaced since it is immutable.

    iii)It is defined under parenthesis().

    iv)Tuples can store any type of element.

for example:- tuple_demo = ('abcd', 123, 4.5, 'pqr', 982.545)

  1. Set:- In Python, a Set is an unordered collection of data types that is iterable, mutable, and has no duplicate elements. The major advantage of using a set, as opposed to a list, is that it has a highly optimized method for checking whether a specific element is contained in the set. The main characteristics of a set are –

    i)Sets are an unordered collection of elements or unintended collections of items In python.

    ii)Here the order in which the elements are added to the set is not fixed, it can change frequently.

    iii)It is defined under curly braces{}

    iv)Sets are mutable, however, only immutable objects can be stored in them.

    for example:- set1 = set([1, 2, 'Hello', 4, 'World', 6])