In this, we study what is the difference between List & tuple & set & dictionary in Python.
- The list is a collection that is ordered and changeable. Allows duplicate members.
- A tuple is a collection that is ordered and unchangeable. Allows duplicate members.
- Set is a collection that is unordered and unindexed. No duplicate members.
- Dictionary is a collection that is unordered, changeable and indexed. No duplicate members.
List & tuple & set & dictionary in Python
In Python, the array starts with 0.
1. List
- The list is a collection that is ordered and changeable. Allows duplicate members.
- In Python list is written with the Square bracket.
- In List support both string and number.
- Add and remove the elements is possible.
Example1:
l= ["zen" , "yen", "ken"]
print(l)
Output
>>['zen', 'yen', 'ken']
Example 2:
l= ["zen", 30 , "yen", 40 ]
print(l)
print(l[2])
print(l[2][0])
print(l[2][-1])
Output
['zen', 30, 'yen', 40]yen
y
n
List Methods
Python has a set of built-in methods that you can use on lists.
Method | Description |
append() | Adds an element at the end of the list |
clear() | Removes all the elements from the list |
copy() | Returns a copy of the list |
count() | Returns the number of elements with the specified value |
extend() | Add the elements of a list (or any iterable), to the end of the current list |
index() | Returns the index of the first element with the specified value |
insert() | Adds an element at the specified position |
pop() | Removes the element at the specified position |
remove() | Removes the item with the specified value |
reverse() | Reverses the order of the list |
sort() | Sorts the list |
2. Tuple
- A tuple is a collection that is ordered and unchangeable. Allows duplicate members.
- In Python, tuples are written with round brackets.
- We cannot add and remove the elements
Example :
l= ("zen", 30 , "yen", 40 )
print(l)
Output
('zen', 30, 'yen', 40)
Tuple Methods
Python has two built-in methods that you can use on tuples.
Method | Description |
index() | Searches the tuple for a specified value and returns the position of where it was found |
count() | Returns the number of times a specified value occurs in a tuple |
3. Set
- Set is a collection that is unordered and unindexed.
- No duplicate members.
- In Python, Set are written with curly brackets.
Example:
l= {"zen", 30 , "yen", 40 }
print(l)
Output
{40, 'yen', 'zen', 30}
Set Methods
Python has a set of built-in methods that you can use on sets.
Methods | Description |
add() | Adds an element to the set |
clear() | Removes all the elements from the set |
copy() | Returns a copy of the set |
difference() | Returns a set containing the difference between two or more sets |
difference_update() | Removes the items in this set that are also included in another, specified set |
discard() | Removes the items in this set that are also included in another, specified set |
intersection() | Returns a set containing the difference between two or more sets |
intersection_update() | Removes the items in this set that are not present in other, specified set(s) |
isdisjoint() | Returns whether two sets have an intersection or not |
issubset() | Returns whether another set contains this set or not |
issuperset() | () Returns whether this set contains another set or not |
pop() | Removes an element from the set |
remove() | Removes the specified element |
symmetric_difference() | Returns a set with the symmetric differences of two sets |
symmetric_difference_update() | inserts the symmetric differences from this set and another |
union() | Return a set containing the union of sets |
update() | Update the set with the union of this set and others |
4. Dictionary
- Dictionary is a collection that is unordered, changeable and indexed.
- No duplicate members.
- In Python, dictionaries are written with curly brackets, and they have keys and values.
Example:
l= {"zen":30, "ben":40 , "yen":50, "ten":60 }
print(l)
Output
{'zen': 30, 'ben': 40, 'yen': 50, 'ten': 60}
Dictionary Methods
Python has a set of built-in methods that you can use on dictionaries.
Methods | Description |
clear() | Removes all the elements from the dictionary |
copy() | Returns a copy of the dictionary |
fromkeys() | Returns a dictionary with the specified keys and value |
get() | Returns the value of the specified key |
items() | Returns a list containing a tuple for each key-value pair |
keys() | Returns a list containing the dictionary’s keys |
pop() | Removes the element with the specified key |
popitem() | Removes the last inserted key-value pair |
setdefault() | Returns the value of the specified key. If the key does not exist: insert the key, with the specified value |
update() | Updates the dictionary with the specified key-value pairs |
values() | Returns a list of all the values in the dictionary |
Source:- w3schools
Also View:- Python Function-lambda |filter, map, reduce.
Also View: – Write a program to create a registration form using AWT.
Thanks for Posting. Keep up the great effort.|