Data Structures in Python
Tags: python, datastructures, algorithms, computerscience
Published: 2020-02-16 15:10:53

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](https://dev-to-uploads.s3.amazonaws.com/i/6supvcnizvnpc3kf0ibj.png "Data Structures in Python")

(ii) Primitive and Non-Primitive Data Structures 
 ![Primitive and Non-Primitive Data Structures](http://res.cloudinary.com/dyd911kmh/image/upload/c_scale,f_auto,q_auto:best,w_700/v1512740202/Template_2_oxrskq.png)

<h3>Primitive Data Structures</h3>

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](https://docs.python.org/3/library/stdtypes.html#string-methods)

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

<h3>Non-Primitive Data Structures</h3>

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!