Search Bar

What is Python and Explain Difference and similarities between List, Tuples, Sets and Dictionary

Python is an easy to learn language that is similar to reading simple English. Python's pseudo-code structure makes it simple for beginners to learn and master. Today, Python is frequently used for a wide range of tasks, including web development, data analysis, scientific computing, artificial intelligence, and more. It has a huge number of standard libraries.

Here is the major difference and similarities between list, Tuples, Sets and Dictionary in python listed:

1.      List: Python Lists are containers to store a set of value of any datatype.

py = [“hello”, 89, True, “Binod”]

· A list is an ordered collection of elements.

· It is mutable, which means we can change its contents (add, remove, modify elements).

· Elements in a list are enclosed in square brackets [ ].

· Lists can contain elements of different data types.

· Lists can allow duplicate elements.

· Example: col = [1, “hi”, 31.06, True, 1]

2     Tuples: A tuple is an immutable data type. If tuple is once defined, it cannot be manipulated.

py = (45, True, “Lion”, 35.26, “snake”)

· A tuple is an ordered collection of elements, similar to a list, but it is immutable.

· Elements in a tuple are enclosed in parentheses ( ). 

· Tuples can contain elements of different data types.

· Tuples are used when we want to store related pieces of data together and ensure that they remain unchanged.

· Tuples can allow duplicate elements.

· Example: tup = (1, “Apple”, 3.14, False, 3.14)

3.      Sets: Set is a collection of non-repetitive elements.

s = {1,6,8,10,67}

· A set is an unordered collection of unique elements. which is immutable.

· It does not allow duplicate values.

· Elements in a set are enclosed in curly braces { }.

· Sets are useful when we want to store a collection of items while ensuring uniqueness.

· Sets are not indexed, and the order of elements is not guaranteed.

· Example: s = {1, 2, 3, 4, 4}

4.      Dictionary: Dictionary is collection of key-value pairs.

dict = {“Name”: “Rohit”

               “Class”: “12”

               “marks”: [68,98,56,87] }

· A dictionary is a collection of key-value pairings, where each key is unique and corresponds to a particular value. Thus, it prevents the use of duplicate values.

· A dictionary's elements are enclosed in curly braces, and a colon : separates each key-value pair

· Dictionary are used when we need to associate data with specific labels (keys) for easy retrieval.

· Dictionaries are not ordered. whereas it is changeable.

· Example: dict = {“name”: “elon”, “age”: 45, “city”: “dallas”}


The major similarities and difference are show in Table below:

Both lists and tuples are ordered collections that allow duplicate values; however, lists can be changed whereas tuples cannot be changed. Sets are unorganized groups that guarantee each element's uniqueness. For effective information organization and extraction, dictionaries are key-value pairs that let us link data with accurate labels. The needs of our program and the features of the data we're dealing with define which data structure we should use.

Post a Comment

0 Comments