DEV Community

Cover image for Python Data Structures: A Comprehensive Guide
seun
seun

Posted on • Updated on

Python Data Structures: A Comprehensive Guide

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]
Enter fullscreen mode Exit fullscreen mode

Characteristics of a List

  1. It is mutable; it allows changes or modification after being created.
  2. It is ordered; it maintains the order of element as it is been added.
  3. It can be combined with other list.
  4. It allows duplication.
  5. it allows a list inside another list which means nestable.
  6. It allows you to store items of various types.
  7. It allows removal and addition of elements.
  8. It accepts iteration.
  9. Elements access can be done in the list using indices.
  10. Elements can be splitted from a list.

Methods used in a List

  1. clear( ): It removes every element on the list
  2. copy( ): It duplicates the list
  3. count( ): It gives back the number of elements present in a list
  4. append( ): It adds an element to the end of a list
  5. index( ): It gives back the specified value through the chosen index
  6. insert( ): It adds an element at the specified position
  7. pop( ): It erases an element of the specified position in the list
  8. sort( ): It helps sort the list
  9. remove( ): It subtracts an element from the list using the specified value
  10. 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)
Enter fullscreen mode Exit fullscreen mode

output

()
(4, 5, 9)
(1, 'Hi', 8.4)
('dog', [6, 4, 3], (5, 7, 9))
Enter fullscreen mode Exit fullscreen mode

Characteristics of Tuple

  1. It is ordered; it follows a sequence as each element is added to the tuple.
  2. 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.
  3. It is immutable; which means the element created cannot be modified or changed upon creation
  4. It accepts duplication of items

Methods used in Tuples

  1. count( ): it displays the specific number of time a value occurred in tuple
  2. 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)
Enter fullscreen mode Exit fullscreen mode

output

{'Halima': 'Awesome', 'John': 'Good', 'Ella': 'Excellent'}
Enter fullscreen mode Exit fullscreen mode

Characteristics of a Dictionary

  1. It is unordered; it doesn’t follow a sequence as the key-values pairs are created
  2. It doesn’t support duplication keys
  3. The identifiers which is known as the key must be special.
  4. It is mutable; it accepts modification or change of data upon creation

Methods used in Dictionary

  1. get( ): It gives the specified key value
  2. clear( ): It removes all elements in the dictionary
  3. copy( ): It duplicates the dictionary
  4. items( ): This returns an index in a tuple form that shows every key-value pair
  5. keys( ): This returns a list containing the dictionary keys
  6. pop( ): It get rid of the element with the specified key
  7. pop item( ): It removes the last inserted key-value
  8. formkeys( ): It returns a dictionary with the specified keys and values
  9. update( ): It updates the dictionary with the specified key-value pairs
  10. setdefault( ): It returns the value of the specified key.
  11. 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)
Enter fullscreen mode Exit fullscreen mode

output

seat ID: {100, 145, 190, 124, 119}
small Letters: {'a', 'b', 'c', 'd', 'e'}
mixed_kind : {'Hi', 'better', 100, -8}
Enter fullscreen mode Exit fullscreen mode

Characteristics of Set

  1. It is mutable
  2. They are unordered
  3. A set element must be unique
  4. It doesn’t allow the use of index

Methods used in Set

  1. copy( ): It helps duplicate the set
  2. add( ): It helps add an element to a set
  3. difference( ): It gives back a set containing the difference between two or more sets
  4. difference update( ): It eliminates the items in the set that are also included in another, particular set
  5. clear( ): It eliminates all the elements in a set
  6. intersection( ): It gives back a set, that is the intersection of two or more sets
  7. discard( ): It eliminates the specified item.
  8. intersection update( ): It eliminates items in a set that are not in another specified set(s)
  9. isdisjoint( ): It shows whether two sets have an intersection or not
  10. issubset( ): It shows whether another set contains a set or not
  11. pop( ): It eliminates an element from a set
  12. remove( ): It eliminates the specified element
  13. symmetric difference( ): It gives back a set with the symmetric difference of two sets
  14. symmetric difference update( ): It gives back the symmetric difference of one set and another
  15. union( ): It gives back a set that entails the union of sets
  16. 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)