Data Structure in Python

In any programming language, Data structure is the specialized format of handling and storing the data. The application that is been developed, should learn the structure of data for the processing.

If you are a computer science student, there would be a separate subject about Data Structure. It’s more of how the data has been handled in a structured format for processing, organizing and storing. There are types in the data structure to represent my data in my program.

Types of Data Structure:

  1. Array
  2. Boolean
  3. String
  4. Integer
  5. Character
  6. Float
  7. Double
  8. Enum
  9. Union
  10. Record
  11. Stack
  12. Queue
  13. Linked List
  14. set
  15. Associative Array
  16. Tree, etc..

Again these types are categorized based on the types of data such as primitives data, composite data & abstract data types.

Python has the special data structure such,

  • List
  • Tuple
  • set
  • dictionary

With all these data structures, let’s see how effective way we can handle the data to represent it in any format.

List:

The list is one of very useful data representation in sequence type to structure the data in Python. Using list we can organize the data in stack, queue & tree format. The list always initiated the index with “0”.

Below the example of how we can use list

 --To initiatlize the list
>>> Emp1=[]    --- Any data represent inside the [] called list.
>>> type(Emp1)
<class 'list'>

The list has many methods to handle the data inside the list as below,

>>> dir(list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

Append: It is a method to add value to the list. Append will add the data at the end of the list.

>>> Emp1.append('emp-a') #Adding employee name
>>> Emp1.append('1001') #employee number
>>> Emp1.append('01-01-2017') #employee data of birth
>>> Emp1.append(1000.00) #salary
>>>
>>>
>>> Emp1
['emp-a', '1001', '01-01-2017', 1000.0]

What if I want to insert the employee department just before the salary, we can use insert method.

Insert: Method to use for insert the data at the specified index in the list as below.

>>> Emp1
['emp-a', '1001', '01-01-2017', 1000.0]
>>> Emp1.insert('Development')
Traceback (most recent call last):
 File "<pyshell#39>", line 1, in <module>
 list.insert('Development')
TypeError: insert() takes exactly 2 arguments (1 given) --- Insert method would expect two argument such index of list where the new data to be added.
>>>
>>> Emp1.insert(3,'Development') -- Insert dept name "development" as 3rd element in Emp1.
>>> Emp1
['emp-a', '1001', '01-01-2017', 'Development', 1000.0]

So here I have noticed one thing is that I could add only one data element to list. But to add all details of an employee in a single execution, we can use extend method.

Extend: Method to add values to the list. Extend will behave same like Append but it can accommodate multiple values to add in one execution.

>>> Emp2 = []     -- Initiated another list Emp2
>>> Emp2.extend('emp-b')  -- Extend the list employee name 'emp-b'
>>> 
>>> Emp2
['e', 'm', 'p', '-', 'b'] -- Oops, It did not act like append. Extend consider input parameter as iteratable. Meaning, it will keep add the given data one by one.
>>> Emp2.append('emp-b')  -- Hope you have noticed the difference between Extend & Append.
>>> 
>>> Emp2
['e', 'm', 'p', '-', 'b', 'emp-b']

So far we have seen how to add the data in the list, now we will play around with data using method Remove, Pop, Del, Index, Count, Sort & Reverse.

>>> Emp1 = ['emp-a','1001','01-01-2017',1000.00]
>>> Emp1.remove(1000.00) --- Wants to remove salary part from this list
>>> Emp1
['emp-a', '1001', '01-01-2017']

#If I wanted to process the Emp1 list and make the list to be empty in order to reuse, then we can use pop method.
>>> Emp1
['emp-a', '1001', '01-01-2017']
>>> Emp1.pop(2) #I wanted to validate DOB of employee and same time clear that from list.
'01-01-2017'

#So before I pop out the items from the list, how can I validate the index of that value.
>>> Emp1.index('1001')  -- Wants to check the index of the employee id in the list.
1
>>> #I have created new list with all employee names
>>> Emp_name = ['a', 'b', 'c', 'd', 'a']
>>> Emp_name
['a', 'b', 'c', 'd', 'a']
>>> #I wanted to check, how many employee has the name "a"
>>> Emp_name.count('a') --Count is the method to count the number of times the data present in the list.
2
>>> #I wanted to order the employee list in ascending.
>>> Emp_name.sort()  -- Sort is the method to sort the data inside the list.
>>> Emp_name
['a', 'a', 'b', 'c', 'd']   -- Data in this list has been sorted albhabetical order. 
>>> #I want to order the employee list in desending.
>>> Emp_name.sort(reverse=True)  -- Sort has the parameter to constomize the elements.
>>> 
>>> Emp_name
['d', 'c', 'b', 'a', 'a']
>>> #List also has the straight forward method to reverse the list.
>>> Emp_name.reverse()
>>> Emp_name
['a', 'a', 'b', 'c', 'd']
>>> #Del has the option to delete the any item from list using the index. But the way of using would slightly vary.
>>> del Emp_name[4] -- Pop method throws the output after deleting it from list but Del wont do that.
>>> 
>>> Emp_name
['d', 'c', 'b', 'a']

The list is very volatile in nature, hence it has all the associated methods to play around the data. You can also create array, stack & queue using list and list can be nested also below,

#List is flexible to add with one or more.
>>> Emp1 = ['A',1,'Class-A']
>>> Emp2 = ['B',2,'Class-B']
>>> Emp3 = ['C',3,'Class-C']
>>> Comp = Emp1 + Emp2 + Emp3 -- Combining the list.
>>> Comp
['A', 1, 'Class-A', 'B', 2, 'Class-B', 'C', 3, 'Class-C']
#Nested List. In another mean, Using list we can create "n" dimentional array.
>>> Comp1 = [Emp1,Emp2,Emp3]  -- Creating the Nested List
>>> Comp1
[['A', 1, 'Class-A'], ['B', 2, 'Class-B'], ['C', 3, 'Class-C']]

Also, inside the list, we can perform any kind of mathematical action to arrive the Complete List.

>>> [x**2 for x in range(10)]   -- Perform for & if loop inside the list.
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Added with that, there are tool in python to perform functional programming on List as below,

>>> #Map function to perform mapper (perform any kind of validation on the given list)
>>> Emp_name
['d', 'c', 'b', 'a']
>>> map(lambda x: x=='a',Emp_name)   -- I wanted to check whether employee 'a' is in the Emp_name list
[False, False, False, True]   -- Return TRUE since the value 'a' present in the last list.

>>> #Filter funtion to perform filter on the list.
>>> filter(lambda x: x=='A', Comp) -- I wanted to get the employee name.
['A']
>>> # Reduce function to perform reduce (combine) operation on list.
>>> Plus_word = ['Welcome ','to ','LEARNINONE.COM']
>>> reduce(lambda x,y:x+y, Plus_word) --reduce the list to one.
'Welcome to LEARNINONE.COM' 

Reduce functionality available under “functions” module on Python version more than 3.

Tuples:

Tuples is another sequence type of data structure in Python. Though tuple is sequence type as like List, it is immutable to play around the data elements inside the tuples. So you need to choose tuple data structure when you want to keep your data be constant through out in the code.

>>> dept_name = 'Computer','Mechanicle','Electrical'
>>> dept_name
('Computer', 'Mechanicle', 'Electrical')
>>> # When you assign multiple values to variable, python will automatically consider that as tuple.
>>> dir(tuple)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
>>> 
>>> dept_name.index('Computer')
0
>>> dept_name.index('Mechanicle')
1
>>> dept_name.count('Electrical')
1

Set:

Set is another sequence type data structure used in Python. Same as tuples, to create the set, we have to use brackets. Unlike tuple & list, set perform the distinct operation on the data elements and remove duplicates.

Note: Python version above 3, uses the curly brackets for sets.

Below example would give you more explanation,

>>> Emp_name = ['a','a','b','c','d']
>>> Emp_name
['a', 'a', 'b', 'c', 'd']
>>> set(Emp_name)  --Removed the duplicates from list Emp_name.
set(['a', 'c', 'b', 'd'])

As like, we can also perform comprehension inside the set.

>>> set(x**2 for x in range(9)) --Finding the power for numbers 1 to 9
set([0, 1, 4, 64, 9, 16, 49, 25, 36])

Dictionary:

Another wonderful data structure is in place for Python programmer is Dictionary. This is to fulfil the associative array data structure type. Dictionary can handle the data that comes with key value pair format as below,

>>> Emp_dept_link={}  --Initializing the dictionary 
>>>
>>> Emp_dept_link
{}    -- Dictionary created with empty key value pair.
>>> dir(Emp_dept_link)   --Modules available to play with key value paid data
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__g
etattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '_
_new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclassho
ok__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'po
pitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues'] 
>>> Emp_dept_link['a']='Computer Science' -- adding new employee 'a' with dept name 'Computer Science'.
>>>
>>> Emp_dept_link
{'a': 'Computer Science'}
>>> Emp_dept_link['b']='Mechanicle' -- adding new employee 'b' with dept name 'Mechanicle'.
>>> Emp_dept_link
{'a': 'Computer Science', 'b': 'Mechanicle'}

with keyword Dict() we can create dictionary on sequence of data type in the form of key value pair as below,

>>> a=[('a','Computer Science'),('b','Mechanicle'),('c','Electrical'),('d','Electronics')]
>>>
>>> a
[('a', 'Computer Science'), ('b', 'Mechanicle'), ('c', 'Electrical'), ('d', 'Electronics')]
>>>
>>> dict(a)
{'a': 'Computer Science', 'c': 'Electrical', 'b': 'Mechanicle', 'd': 'Electronics'}
>>> dict(a=1001, b=1002, c=1003,d=1004)  -- Assign the employee id value to employee name.
{'a': 1001, 'c': 1003, 'b': 1002, 'd': 1004}

As like List & set, Dict() comprehension can be used to create the dictionary from arbitrary key & value expression as below.

>>> {x: x**2 for x in range(9)}  
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}

We will further deep look into the upcoming articles about List, Tuple, Set & Dictionary

1 thought on “Data Structure in Python”

Leave a Reply