DEV Community

Faith Mueni Kilonzi
Faith Mueni Kilonzi

Posted on

Data Structures in Python

DATA STRUCTURES IN PYTHON
You can never be an efficient programmer without mastering data structures and algorithms. Problem solving in programming is impossible without the basics. Regardless of which aspect of python programming you want to focus on, you can’t ignore data, its organization and the algorithms you need to find solutions to their manipulation and data analysis.
Data structures are a way of organizing data in such a way that you can store collections of data, relate the, and perform operations on them. Data structures are derived from the term: data abstraction which is the use of data structures to solve problems by focusing on the main picture without getting lost in the details of meta-data and data access. They ease the process of problem-solving and ensure efficiency.
In other words, data structures are an actual implementation of the Abstract Data Types (ADT). This implementation requires you to have a physical view (how data collections are stored and organized) in a particular programming language’s constructs and basic data types.
There are two ways of grouping data structures in python:
(i) Built-in and User-Defined Data Structures

Data Structures in Python

(ii) Primitive and Non-Primitive Data Structures
Primitive and Non-Primitive Data Structures

Primitive Data Structures

  1. Integers : Used to represent numeric data, more specifically whole numbers from negative infinity to infinity, like 2,3, -100000, 10000.
  2. Float : ‘Float’ stands for floating point number. It is used for rational numbers usually ending with decimal figure such as 1.1,2.3,9.3 etc.
  3. String : Strings are a collection of alphabets, words or other characters. In Python, strings are created by enclosing a sequence of characters within a pair of double or single quotes. x= “Hello World”. Explore the different string operations here

  4. Boolean : It is a built-in data type that can take the values TRUE or FALSE

Non-Primitive Data Structures

This section covers some of the primitive (and built-in) Python Data Structures.

  1. Lists
    Stores indexed elements that are changeable and can contain duplicate items.
    Mutable, ordered series, traditionally of the same type of object.
    Advantages: Mutable and ordered. Easy to understand. Relatively efficient memory usage.
    Disadvantages: Searching is O(n).
    To create a list, use square brackets:
    mylist = [ ]
    mylist = [1,2,3]
    mylist = ['a', 'b', 'c', [1,2,3] ] # 4 elements

  2. Tuples
    Stores indexed, unchangeable elements that can have duplicate copies.
    Immutable, ordered series traditionally containing different objects
    Advantages: Immutable and ordered. Relatively efficient memory usage (more than lists).
    Disadvantages: Searching is O(n). Hard to understand for many Python newcomers.

  3. Dictionaries
    Mutable, unordered pairs (keys and values) of objects. Keys must be hashable.
    Advantages: O(1) searching for keys. Makes it easy to create trees and other hierarchical data structures. Can be used to create self-documenting code. Many problems can be described in terms of key-value pairs.
    Disadvantages: Only lookup by key. Uses more memory than lists and tuples. Keys must be hashable.

  4. Sets
    A set is a collection which is unordered and unindexed. In Python sets are written with curly brackets.
    Once a set is created, you cannot change its items, but you can add new items.
    Advantages:Relatively efficient memory usage (more than lists).
    Disadvantages: Sets are unordered, so you cannot be sure in which order the items will appear.Searching is O(n).

We will cover each of the data structures in the next tutorials. Thank you for reading!

Top comments (8)

Collapse
 
safinghoghabori profile image
Safin Ghoghabori

What is the difference between list and tuple? In same problem we can use either both of them?

Collapse
 
fronkan profile image
Fredrik Sjöstrand • Edited

Tuples are immutable while lists are mutable. This for example has the effect that tuples are hashable and therefore, can be used in sets and as keys in dictionaries.
Also, this makes lists the better option when you need to addi or remove items. If you doesn't, as mentioned in the article, the tuples has lower memory footprint.

Collapse
 
safinghoghabori profile image
Safin Ghoghabori

Ohk thanks for your warm reply

Collapse
 
kingtomi profile image
Ayodabo Oluwatomisin

List are mutable non primitive data Structures while tuple is the opposite, immutable. You make use of list when you will be modifying the contents from time to time, you use tuples when you don't need to modify it after creating it. It places a restriction and helps makes computation faster.

Collapse
 
global_codess profile image
Faith Mueni Kilonzi

Nice explanation.

Collapse
 
ianakotey profile image
ianakotey

Just a little clarification. Sets are mutable and unordered

Collapse
 
global_codess profile image
Faith Mueni Kilonzi

Thank you for the clarification. I made the change.

Collapse
 
vincenttommi profile image
Mr #1738

educative alot__