top of page

Python Data-Structures

  • Writer: Jatin Madaan
    Jatin Madaan
  • Jul 19, 2019
  • 3 min read

Updated: Aug 7, 2019


Data structure is a collection of data elements (such as numbers or characters or even other data structures) that is structured in some way eg : numbering the elements .


Types of Data Structures :


1 - Lists :

  • List is sequence data structure.

  • It is Collection of items ie Strings , int or other lists .

  • Enclosed in [] (square brackets).

  • Each item has assigned index value.

  • Each item is separated by comma.

  • Lists are mutable ie values can be changed.

List operations :

a - List creation :


ree


b - List length :



ree

c - List Append :



ree

d - List Insert :



ree

e - List Remove :


ree

f - List Extend :


ree

g - List Delete :


ree

h - List Keywords :


ree

i - List Reverse :


ree

j - List Sorting :

- Returns a new list in sorted order.

- Original list not changed.

- Optional argument (reverse=True).

- In-place sorting can also be done.


ree


k - List with multiple references :


ree

l - String split :


ree

m - List Indexing :

- Each item in list has an assigned index value starting from 0.

- Accessing element in a list is called indexing .


ree

n - List Slicing :

- Accessing part of segments is called slicing .

- end value represents that the first value that is not present in the selected slice

eg : numbers[start:end:step_size].


ree

o - List extend using + :


ree

p - List Count :


ree

q - List Looping :


ree

r - List Comprehensions :

- Provides concise way to create lists.

- Common applications : are to make new list where each element is result of some operation applied to each member of another sequence or iterable or to create a sequence of those elements to satisfy a certain condition .

eg:

ree

s - Nested List Comprehensions :



ree


2 - Tuples

  • It is similar to list and enclosed in () .

  • Tuples are immutable ie elements cannot be changed or values cannot be updated.

Tuple operations :


a - Tuple Creation :


ree


b - Accessing elements in Tuple creation :


ree

c - Changing a Tuple :

- Tuples are immutable.

- If element is itself of mutable datatype like list , it's nested items can be changed.


ree


d - Tuple Deletion :

We cannot update/delete elements/items from a tuple but we can delete/remove entire tuple.


ree


e - Tuple Count :


ree


f - Tuple Index :


ree

g - Tuple Membership :

Tests if value exists or not and returns True/False.


ree


h - Tuple Length :


ree


i - Tuple Sort :


ree

3 - Sets

  • They are unordered collection of items.

  • Every item is unique(no duplicates).

  • Sets are mutable ie we can add/remove items from it .

  • Cannot be indexed as not ordered.

  • Mathematical operations like union , intersection etc can be performed.

Sets operations :


a - Set Creation :


ree

b - Add elements to Set :

- We can add single element using add() method and multiple elements via update(). method.


ree

c - Remove elements from Set :

- methods :- remove(),discard(),pop(),clear.


ree

d - Mathematical Set operations:


ree

Frozen Sets :

- They have characteristics of set , but cannot be changed once assigned.

- Frozen sets are immutable sets.

- They are hash-able & can be used as keys to dictionary (normal sets cannot).

- Methods - copy() , difference(),intersection(), isdisjoint(), issubset(), issuperset(), symmetric_difference() & union().

- Does not have a method to add/remove elements since it is immutable.


ree


4 - Dictionary

  • It is unordered collection of items.

  • Cannot be indexed.

  • Dictionary has key:Value pair .

Dictionary operations :


a - Dictionary Creation :


ree

b - Dictionary Access :

- Internally hash function is used so access is very fast.


ree

c - Dictionary Add/Modify :


ree

d - Dictionary Delete/Remove :


ree

e - Dictionary Methods :


ree

f - Dictionary Comprehension :


ree


5 - Strings

  • String is a sequence of characters.

  • Computers don't understand characters they understand only binary numbers (Internally char is manipulated as 0's and 1's).

  • Conversion of a char to 0's and 1's is call encoding and reverse is decoding.

  • In Python string is a sequence of unicode characters (utf-8).

String operations :


a - String Creation :

- Single quotes, double quotes , triple quotes(generally used to represent multi-line string , docStrings and comments).


ree

b - String Access :

- Using index access it .

- Index starts from 0.

- If out of index then error.

- Index must be integer.

- Negative indexes are allowed.


ree

c - String Delete/change :

- They are immutable .

- Elements cannot be changed.

- We can reassign different strings to same name.


ree

d - String Operations :


ree

e - String Iteration :


ree

f - String Membership :


ree

g - String Methods :


ree

Comments


  • linkedin

©2019 by Jatinmadaan

bottom of page