difference between list, tuple, set, dictionary

Difference Between List, Tuple, Set, And Dictionary In Python

Hello guys, welcome back to our blog. In this article, we will discuss the difference between list, tuple, set and dictionary in python, we will also share the code of list, tuple, set, and dictionary in python.

If you have any electrical, electronics, and computer science doubts, then ask questions. You can also catch us on Instagram – CS Electrical & Electronics.

Also, read:

Difference Between List, Tuple, Set, And Dictionary

List

fruits = ['Orange', 'Apple', 'Pear', 'Banana', 'Kiwi', 'Apple', 'Banana']
print(fruits)

Output:

['Orange', 'Apple', 'Pear', 'Banana', 'Kiwi', 'Apple', 'Banana']

The list is a comma-separated item enclosed in square braces. A list is mutable means in the list we can change elements. Let’s do it.

fruits = ['Orange', 'Apple', 'Pear', 'Banana', 'Kiwi', 'Apple', 'Banana']
list[0] = "Grape"
print(fruits)

Output:

['Grape', 'Apple', 'Pear', 'Banana', 'Kiwi', 'Apple', 'Banana']

Now we can see that the value of item 0 is changed. In list, we can change the item or element.

Methods performed on lists

  1. append() - adds an element at the end of the list
  2. insert() - adds an element at the specified position
  3. extend() - adds the elements of a list ( or any iterable), to the end of the current list
  4. copy() - returns a copy of the list
  5. count() - retuns the number of elements with the specified value
  6. clear() - removes all the elements from the list
  7. index() - returns the index of the first element with the specified value
  8. remove() - removes the item with the specified value
  9. pop() - removes the order of the list
  10. reverse() - reverses the order of the list
  11. sort() - sorts the list

Tuple

tuple = ("Chetan","Nitin",23,"Ratan",44)
print(tuple)

Output:

("Chetan","Nitin",23,"Ratan",44)

A tuple is also a comma-separated item but enclosed in round braces. A tuple is immutable means in a tuple we can’t change the elements. Let’s do it.

tuple = ("Chetan","Nitin",23,"Ratan",44)
tuple[0] = "Shidling"
print(tuple)

Output:

You will get errors like this

TypeError: 'tuple' object does not support item assigned

So, in a tuple, we can’t change the element.

Set

The Set uses the keyword “set” with comma-separated items enclosed with both square & round braces and the set can also be created with curly braces. The set is an unordered collection with no duplicate elements. Curly braces or the set() function can be used to create sets.

Set with square and round braces

set = set(["Computer", "Printer", "TV", "Camera", 420])
print(set)

Output:

{'Computer', 'Printer', 'TV', 'Camera', 420}

It will print output in curly braces.

Set with curly braces

basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana','watermelon'}
print(basket)
type(basket)

Output:

{'banana', 'orange', 'apple', 'pear', 'watermelon'}
set

Dictionary

A set of key:value pairs – with the requirement that the keys are unique (within one dictionary). dictionaries are indexed by keys, which can be immutable.

diction_data = {'Name':'Chetan', 'Age':21, 'Height':'5.9 ft', 'Hobby':'Blogging'}
print(diction_data)
type(diction_data)

Output:

{'Name': 'Chetan', 'Age': 21, 'Height': '5.9 ft', 'Hobby': 'Blogging'}
dict

This article was about the difference between list, tuple, set, and dictionary in python. I hope this article may help you all a lot. Thank you for reading.

Also, read:

About The Author

Share Now