Python Data Structures:
Data structures refer to the collection or group of data in a particular structure.
Below table illustration provides the information on python list, tuple, dictionary and set declaration and predefined methods.
Type: |
List |
Tuple |
Dictionary |
Set |
Functionality/Definition: |
àPython List is a collection of ordered elements.
àList values can be updated.
àAllows duplicate values. |
àPython tuple is a collection of ordered elements, but values cannot be updated.
àAllows duplicate values |
à Python Dictionary is a collection of unordered elements which are indexed.
àValues can be Updated.
àDoesn’t allow duplicate values. |
àPython set is a collection of unordered elements but are unindexed.
àDoesn’t allow duplicate values. |
Declaration: |
[]
Example: list = ["IN", "US", "GB"]
|
()
Example:
tuple = ["IN", "US", "GB"]
|
{}
Example:
dict = { }
|
{}
Example:
set= {1,2,3} Or set=set ([1,2,3]) |
Methods and syntax: |
append() list.append(item)
clear() list.clear()
copy() list2 = list.copy()
count() list.count(element)
extend() list.extend(list2)
index() list.index(element)
insert() list.insert(index, element)
pop() list.pop(index) remove() list.remove(element)
reverse() list.reverse()
sort() list.sort()
|
count() tuple.count(element)
index() tuple.index(element)
|
clear() dict.clear()
copy() dict.copy()
fromkeys() dict=dict.fromkeys(x,y)
get() dict.get("Locale")
items() dict.items()
keys() dict.keys()
pop() dict.pop("Country”)
popitem() dict.popitem()
setdefault() dict.setdefault("Country”,"IN",)
update() dict.update("Locale ","es_MX”)
values() dict.values()
|
add() set.add("IN")
clear() set.clear()
copy() set.copy()
difference() set.difference(set2)
diifernce_update() set.difference_update(set2)
discard() set.discard(value)
intersection() set.intersection(set2)
intersection_update() set.intersection_update(set2)
isdisjoint() set.isdisjoint(set2)
issubset() set.issubset(set2)
issuperset() set.issuperset(set2)
pop() set.pop()
remove() set.remove(item)
symmetric_difference() set.symmetric_difference(set2)
symmetric_difference_update() set.symmetric_difference_update(set2)
union() set.union(set2)
update() set.update(set2)
|