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:
- Top 20 Interview Questions On RTOS, Real-Time Operating System
- Bit Operation In C Programming With Example & Applications
- Top 14 Websites To Download Source Code For Free
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
append() - adds an element at the end of the list
insert() - adds an element at the specified position
extend() - adds the elements of a list ( or any iterable), to the end of the current list
copy() - returns a copy of the list
count() - retuns the number of elements with the specified value
clear() - removes all the elements from the list
index() - returns the index of the first element with the specified value
remove() - removes the item with the specified value
pop() - removes the order of the list
reverse() - reverses the order of the list
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:
- 10 Tips To Maintain Battery For Long Life, Battery Maintainance
- 10 Tips To Save Electricity Bills, Save Money By Saving Electricity
- 100 (AI) Artificial Intelligence Applications In The Automotive Industry
- 100 + Electrical Engineering Projects For Students, Engineers
- 100+ C Programming Projects With Source Code, Coding Projects Ideas
- 1000+ Control System Quiz, Top MCQ On Control System
- 1000+ Electrical Machines Quiz, Top MCQs On Electrical Machines
- 1000+ Electronics Projects For Engineers, Diploma, MTech Students
- 1000+ Interview Questions On Java, Java Interview Questions, Freshers
- 1000+ MATLAB Simulink Projects For MTech, Engineering Students