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 listinsert() - adds an element at the specified positionextend() - adds the elements of a list ( or any iterable), to the end of the current listcopy() - returns a copy of the listcount() - retuns the number of elements with the specified valueclear() - removes all the elements from the listindex() - returns the index of the first element with the specified valueremove() - removes the item with the specified valuepop() - removes the order of the listreverse() - reverses the order of the listsort() - 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:
- “Mother of All Deals”: How The EU–India Free Trade Agreement Can Reshape India’s Economic Future
- 10 Free ADAS Projects With Source Code And Documentation – Learn & Build Today
- 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
- 100+ Indian Startups & What They Are Building
- 1000+ Automotive Interview Questions With Answers
- 1000+ Electronics Projects For Engineers, Diploma, MTech Students
