Basic Of Python: Data Structure (Part 1)
Data structure refers to the way data is organized, stored, and manipulated in a computer so that it can be efficiently accessed and used. Think of it as the framework or blueprint for managing data effectively. It involves techniques for organizing and managing data in a manner that allows for efficient operations like insertion, deletion, searching, and sorting.
In simpler terms, imagine you have a bunch of books. How you arrange them on your shelf, how you label them, and how you access them when needed, all constitute a kind of data structure. Similarly, in computer science, data structures are the mechanisms that enable efficient storage and retrieval of data in various applications.
In Python, you have access to a wide range of built-in data structures that you can use to organize and manipulate data efficiently. Some of the most commonly used data structures in Python include:
1. Lists:
- Lists are ordered collections of items, which can be of mixed types (integers, strings, etc.).
- They are mutable, meaning you can change their content after creation.
- Lists are indexed, meaning you can access elements by their position.
- Lists are denoted by square brackets [ ].
2. Tuples:
- Tuples are similar to lists, but they are immutable, meaning their content cannot be changed after creation.
- They are often used to represent fixed collections of items, such as coordinates or settings.
- Tuples are denoted by parentheses ( ).
3. Dictionaries:
- Dictionaries are collections of key-value pairs, where each key is associated with a value.
- They are unordered, meaning the order of items is not guaranteed.
- Dictionaries are useful for storing and retrieving data based on a unique key.
- They are denoted by curly braces { }.
4. Sets:
- Sets are unordered collections of unique elements.
- They are useful for tasks such as removing duplicates from a list or testing membership of an element.
- Sets do not allow duplicate elements, so adding the same element multiple times has no effect.
- Sets are denoted by curly braces { } or by using the set() function.
5. Arrays:
- Arrays are collections of items of the same type.
- They are provided by the array module in Python and offer efficient storage of homogeneous data types.
- Arrays are useful for numerical computations and when memory efficiency is important.
import array
my_array = array.array(‘i’, [1, 2, 3, 4, 5])
6. Deque:
- Deques (double-ended queues) are optimized for inserting and removing items from both ends of a collection.
- They provide fast operations for adding and removing elements from either end.
- Deques are provided by the collections module.
from collections import deque
my_deque = deque([1, 2, 3, 4, 5])
7. String:
- Ordered collection of characters.
- Immutable: Characters cannot be modified after creation.
- Accessed by index.
my_string = ‘Hello, World!’
8. Bytes & Byte Arrays:
- Immutable and mutable sequences of bytes, respectively.
- Used for storing binary data.
my_bytes = b’hello’
my_bytearray = bytearray(b’hello’)
7. Stack and Queue:
- While Python does not have built-in classes for stacks and queues, you can easily implement them using lists.
- Stacks follow the Last In, First Out (LIFO) principle, where the last element added is the first one to be removed.
- Queues follow the First In, First Out (FIFO) principle, where the first element added is the first one to be removed.
These data structures serve different purposes and have different characteristics, so choosing the right one depends on the specific requirements of your problem.
Lists:
Example:
my_list = [1, 2, 3, 4, 5]
Commonly used functions/methods:
- append(): Adds an element to the end of the list.
Check the line number 8, where i have appeneded a new List, it has added a new list inside existing list. here its, [7,8,9], in the last index. Understand this, how append() works with list and check next another list based function extend() and understand difference.
- extend(): Extends the list by appending elements from another list.
Here, i am using same statement to add new list, but using extend() function. You can see, it has added 3 new values in 3 new index, where last time in append function it was one index contains a whole list value.
Check this, what happend if we will extend a list with an element (with primitive type data):
In above code, i tried to extend one integer in existing list and it throws an error. Because, extend() function allow only to add another iterator function not primitive type.
- pop(): Removed and returns the last element of the list.
- insert(): Inserts an element at a specified position.
Syntex: insert(index where you have to add new element, the new value to insert)
- remove(): Removes the first occurrence of a specified value.
If you want to remove all occurrance of 20 from list, need to use below defined different way.
- index(): Returns the index of the first occurrence of a specified value.
You can see on above example index() function returned only first occurrence of value 20, though value 20 is also exists on index 4.
You can use loop to read indexs of multiple occurrance of specified elements.
- count(): Returns the number of occurrences of a specified value.
- Other calculations in list, like add two list, multiply a list.
What you can’t do with list?
- You can’t add a list with primitive datatypes, like my_list + 4.
- You can’t multiply two list directly, like my_list1 * my_list2.
- You can’t directly devide and subtract a list with any other list or any primitive datatype.
Tuples:
Example:
my_tuple = (1, 2, 3, 4, 5)
Tuples’s approx all functions are same as List, but because Tuples are immutable (unchangable), it doesn’t have functions to update an existing Tuples, like append(), extend() or remove(). But count() and index() has same logic.
- Concatination and repetition are allowed in tuples.
- Packing and Unpacking a List and Tuples
Dictionaries:
Example:
my_dict = {‘a’: 1, ‘b’: 2, ‘c’: 3}
How to add new items in an existing dictionary?
Commonly used functions/methods:
- get(): Returns the value associated with the specified key. If the key is not found, it returns a default value (None by default).
From above example you can see how to retrieve the value from a specified key in different way and how to return a default value if key does not exists.
- keys(): Returns a view object (dict_keys class) that displays a list of all the keys in the dictionary.
- values(): Returns a view object (dict_values class) that displays a list of all the values in the dictionary.
- items(): Returns a view object that displays a list of all key-value pairs in the dictionary as tuples.
Mostly when you program and need to read all key and value for any logical requirements, you can iterate items and read key and value both, like below eaxmple.
- pop(): Removes the item with the specified key from the dictionary and returns its value. Raises a KeyError if the key is not found.
In above example, ypu can see, in line number 3, i have asked to pop key ‘a’ from my_dict. The returned value ret is 1, which is value of key ‘a’. And after pop when i printed my_dict its showing updated value, mean after pop you can’t use key ‘a’ from my_dict. And another example, what happens when trying to delete some key doesnot exists.
- popitem(): Removes and returns the last key-value pair inserted into the dictionary as a tuple. Raises a KeyError if the dictionary is empty.
- update(): Updates the dictionary with the key-value pairs from another dictionary or an iterable of key-value pairs.
- clear(): Removes all items from the dictionary.
Set:
Example:
my_set = {1,2,3} or set([1,2,3])
Sets in Python are unordered collections of unique elements. Here are some of the most commonly used basic functions and methods for working with sets:
- add(): Adds an element to the set.
- remove(): Removes a specified element from the set. Raises a KeyError if the element is not present.
- discard(): Removes a specified element from the set if it is present, but does not raise an error if the element is not present.
- clear(): Removes all elements from the set.
- copy(): Returns a shallow copy of the set.
What is difference bewteen new_set = my_set.copy() and new_set = my_set?
Some Set related operations:
- union(): Returns a new set containing all unique elements from both sets.
- intersection(): Returns a new set containing elements that are common to both sets.
- difference(): Returns a new set containing elements that are present in the first set but not in the second set.
- symmetric_difference(): Returns a new set containing elements that are present in either set, but not in both.
Example:
Slicing List & Tuples
Slicing is a technique used to extract a portion of a sequence (like lists, tuples, and strings) in Python. Lists and tuples support slicing using the list[start:stop:step] syntax, where start, stop and step are optional parameters.
Sets and dictionaries do not support slicing directly, but you can achieve similar functionality using other techniques like iteration or comprehension.
Example of slicing on list:
You can use same for the tuples:
Here, need to understand. When using positive value in slicing, its start from index 0. As line number 5, 1:4 mean - read data from index number 1 to index number 3, exclude index 4. You do not have to read the stop index part. If there is no number mean start index is 0. Like line number 13, ie. [:3] — it is saying, get element from starting (index 0) to index 2, exclude 3. If it is [3:] — Mean, get element from index 3 to end, mean all. It will not exclude the last one.
Now, What is negative slicing. Negative slicing start from -1, is in reverse order started from last index. Where last index is -1. So if it is [-3:-1], mean get element from third last index to second last index, exclude last index. Based on above example line number 9, -3 index of my_list array has value ‘3’ and second last index which is -2 has 4, by excluding given -1. So it will return [3,4].
If you have to reverse a list, tuples or string, just put -1 in step portion.
Example-: my_list[::-1]
What will not return any data?
- If slicing with same index — my_list[1:1] or my_list[-2:-2]
- if slicing with reverse positive index — my_list[2:1]
- if slicing negative as forward indexing — my_list[-1:-2]
To understand more slicing, practice more.
Its not end
List, Tuples, Dict and Set are the basic data structure of Python. I have shared all basic operation here with you.
But there is more.
Advance Data Structure, what i will share with you in next article (Part 2). It is already very long article so that.
You wanna learn more ? Check the references.
Happy coding! 🚀👩💻 & Sorry for any Grammarly mistakes.