What is Python Data Structures?
This can be defined as a unique way of storing and arranging data. They are data types in which data is represented and it ensures access to the data as well.
There are 4 types of data structure in pyhton namely:
1) List
2) Dictionary
3) Tuple
4) Sets
List
Python list can be used to store many items in one variable. A square bracket “[ ]” is used to create a list in python. List allows you to store items of any type and also allows a combination of different data types such as string, float, Boolean and an object.
Examples on list
marks = [10, 26, 24]
print(marks)
# Output: [10, 26, 24]
Characteristics of a List
- It is mutable; it allows changes or modification after being created.
- It is ordered; it maintains the order of element as it is been added.
- It can be combined with other list.
- It allows duplication.
- it allows a list inside another list which means nestable.
- It allows you to store items of various types.
- It allows removal and addition of elements.
- It accepts iteration.
- Elements access can be done in the list using indices.
- Elements can be splitted from a list.
Methods used in a List
- clear( ): It removes every element on the list
- copy( ): It duplicates the list
- count( ): It gives back the number of elements present in a list
- append( ): It adds an element to the end of a list
- index( ): It gives back the specified value through the chosen index
- insert( ): It adds an element at the specified position
- pop( ): It erases an element of the specified position in the list
- sort( ): It helps sort the list
- remove( ): It subtracts an element from the list using the specified value
- reverse( ): It reverses the order of the list
Tuple
It is used for storing all kinds of values attached to a variable. This is done by putting the items in a bracket( ). It cannot be modified after being created and new items cannot be added upon creation. It can be used to store different data type for items.
Examples on Tuple
# Different types of tuples
# Empty tuple
my_cranes = ()
print(my_cranes)
# Tuple having integers
my_cranes = (4, 5, 9)
print(my_cranes)
# tuple with mixed datatypes
my_cranes = (1, "Hi", 8.4)
print(my_cranes)
# nested tuple
my_cranes = ("dog", [6, 4, 3], (5, 7, 9))
print(my_cranes)
output
()
(4, 5, 9)
(1, 'Hi', 8.4)
('dog', [6, 4, 3], (5, 7, 9))
Characteristics of Tuple
- It is ordered; it follows a sequence as each element is added to the tuple.
- It allow indexing; ability to access the elements using an index which can either be positive or negative. This index shows the position of each element in a tuple.
- It is immutable; which means the element created cannot be modified or changed upon creation
- It accepts duplication of items
Methods used in Tuples
- count( ): it displays the specific number of time a value occurred in tuple
- index( ): it returns the position of a specific value in a tuple
Dictionary
This is a type of data structure that stores its items in key-value pairs. It is represented using a curly braces ”{ }” along with a colon “ : ” that separates the key from the value that is attached to it. A key is what identifies the item, while the value is what is attached to the key.
Examples on Dictionary
# creating a dictionary
name_remarks = {
"Halima": "Awesome",
"John": "Good",
"Ella": "Excellent"
}
# printing the dictionary
print(name_remarks)
output
{'Halima': 'Awesome', 'John': 'Good', 'Ella': 'Excellent'}
Characteristics of a Dictionary
- It is unordered; it doesn’t follow a sequence as the key-values pairs are created
- It doesn’t support duplication keys
- The identifiers which is known as the key must be special.
- It is mutable; it accepts modification or change of data upon creation
Methods used in Dictionary
- get( ): It gives the specified key value
- clear( ): It removes all elements in the dictionary
- copy( ): It duplicates the dictionary
- items( ): This returns an index in a tuple form that shows every key-value pair
- keys( ): This returns a list containing the dictionary keys
- pop( ): It get rid of the element with the specified key
- pop item( ): It removes the last inserted key-value
- formkeys( ): It returns a dictionary with the specified keys and values
- update( ): It updates the dictionary with the specified key-value pairs
- setdefault( ): It returns the value of the specified key.
- values( ): It returns the list of all values in the dictionary
Set
This is a type of data structure that is used to store many items in a variable. It can be created using curly braces “{ }” or using the set( ) method to as well.
Examples on Sets
# create a set of integer type
seat_id = {100, 145, 190, 124, 119}
print('Seat ID:', seat_id)
# create a set of string type
small_letters = {'a', 'b', 'c', 'd', 'e'}
print('small Letters:', small_letters)
# create a set of mixed data types
mixed_kind = {'Hi', 100, -8, 'better'}
print('mixed kind:', mixed_kind)
output
seat ID: {100, 145, 190, 124, 119}
small Letters: {'a', 'b', 'c', 'd', 'e'}
mixed_kind : {'Hi', 'better', 100, -8}
Characteristics of Set
- It is mutable
- They are unordered
- A set element must be unique
- It doesn’t allow the use of index
Methods used in Set
- copy( ): It helps duplicate the set
- add( ): It helps add an element to a set
- difference( ): It gives back a set containing the difference between two or more sets
- difference update( ): It eliminates the items in the set that are also included in another, particular set
- clear( ): It eliminates all the elements in a set
- intersection( ): It gives back a set, that is the intersection of two or more sets
- discard( ): It eliminates the specified item.
- intersection update( ): It eliminates items in a set that are not in another specified set(s)
- isdisjoint( ): It shows whether two sets have an intersection or not
- issubset( ): It shows whether another set contains a set or not
- pop( ): It eliminates an element from a set
- remove( ): It eliminates the specified element
- symmetric difference( ): It gives back a set with the symmetric difference of two sets
- symmetric difference update( ): It gives back the symmetric difference of one set and another
- union( ): It gives back a set that entails the union of sets
- update( ): It helps to update a set with another set, or any other iterable
Conclusion
Congratulations on a hanging around from the beginning to the end. I hope this article has been able to simplify data structures in python and enjoyed reading article as well. I wish you good luck in tech journey.
Top comments (0)