Python Language Tutorial Part- 3 (Data Structure in List)

Today, I took a new part of python. In this Part we will see the usage of list and how can use multiple ways for arranging data in list. All the information and points are interesting in this part of the tutorial.

I have already told you.

Download Python LanguageTutorials and give star on GitHub if that files are helpful for you.

In this article you will understand the following:

  • List
  • Stack
  • Queue

The concepts you may have known theoretically, but today I’ll explain how we can implement these concepts in python with simple code implementation.

List

List is an important data type in Python. It stores many data. In Python, List has many function that make easy and simple for handling list in python.

  • list.append(X) : Add an item to the end of the list.
  • list.extend(L): Extend the list by appending all the items in the given list.
  • list.insert(I, X) : Insert an item at a given position.
  • list.remove(X) :Remove the first item from the list.
  • list.pop([I]) : Remove the item at the given position in the list.
  • list.clear() : Remove all items from the list.
  • list.index(X) : The index in the list of the first item whose value is X.
  • list.count(X) : The number of times X appears in the list.
  • list.sort() : Sort the items of the list in place.
  • list.reverse() : Reverse the elements of the list in place.
  • list.copy() : A copy of the list.

Now see the example below:

Example 1:

  1. #!/usr/bin/python  
  2. # -*- coding: utf-8 -*-  
  3. a = [  
  4.     1,  
  5.     4,  
  6.     3,  
  7.     6,  
  8.     3,  
  9.     5,  
  10.     6,  
  11.     74,  
  12.     6,  
  13.     4,  
  14.     3,  
  15.     6,  
  16.     2,  
  17.     8,  
  18.     ]  
  19.   
  20. # number of times values appears in the list.  
  21.   
  22. print (  
  23.     'a.count(3)=',  
  24.     a.count(3),  
  25.     'a.count(6)=',  
  26.     a.count(6),  
  27.     "a.count('x')=",  
  28.     a.count('x'),  
  29.     )  
  30.   
  31. # Insert an item at a given position  
  32.   
  33. a.insert(2, -1)  
  34. print ('a.insert(2, -1)=', a)  
  35.   
  36. # Add an item to the end of the list  
  37.   
  38. a.append(3)  
  39. print ('a.append(3)=', a)  
  40.   
  41. # the index in the list of the first item . It is an error if there is no such item  
  42.   
  43. print ('a.index(3)=', a.index(3))  
  44.   
  45. # Remove the first item from the list.. It is an error if there is no such item  
  46.   
  47. a.remove(3)  
  48. print ('a.remove(3)=', a)  
  49.   
  50. # Reverse the elements of the list  
  51.   
  52. a.reverse()  
  53. print ('a.reverse()=', a)  
  54.   
  55. # Sort the items of the list  
  56.   
  57. a.sort()  
  58. print ('a.sort()=', a)  
  59.   
  60. # removes and returns the last item in the list.  
  61.   
  62. print ('a.pop()=', a.pop())  
  63.   
  64.               
Output: 

    a.count(3)= 3 a.count(6)= 4 a.count('x')= 0
    a.insert(2, -1)= [1, 4, -1, 3, 6, 3, 5, 6, 74, 6, 4, 3, 6, 2, 8]
    a.append(3)= [1, 4, -1, 3, 6, 3, 5, 6, 74, 6, 4, 3, 6, 2, 8, 3]
    a.index(3)= 3
    a.remove(3)= [1, 4, -1, 6, 3, 5, 6, 74, 6, 4, 3, 6, 2, 8, 3]
    a.reverse()= [3, 8, 2, 6, 3, 4, 6, 74, 6, 5, 3, 6, -1, 4, 1]
    a.sort()= [-1, 1, 2, 3, 3, 3, 4, 4, 5, 6, 6, 6, 6, 8, 74]
    a.pop()= 74

Example 2:

In this example you will see how to calculate Square of each list value:

  1. square=[(x, x**2for x in range(6)]  
  2. #print square of each value  
  3. print(square)  
  4. square_list = [x**2 for x in range(15)]  
  5. #print square of each value  
  6. print(square_list)  
Output: 

    [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]
    [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196]

Example 3:

In this example you will see how to combine two lists.

  1. comp_list=[(x, y) for x in [1,2,3for y in [4,5,6if x != y]  
  2. print(comp_list)  
  3. combs = []  
  4. for x in [1,2,3,]:  
  5. for y in [4,5,6]:  
  6. if x != y:  
  7. a. combs.append((x, y))  
  8. print(combs)  
  9.               
Output: 

    [(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]
    [(1, 4), (1, 5), (1, 6)]
    [(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6)]
    [(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]

Example 4:

In this example you will see how to get element in List.

  1. vec = [[1,2,3], [4,5,6], [7,8,9]]  
  2. elem=[num for elem in vec for num in elem]  
  3. print(elem)  
Output: 

    [1, 2, 3, 4, 5, 6, 7, 8, 9]

Example 5:

In this example you will see use of ‘pi’:

  1. #!/usr/bin/python  
  2. # -*- coding: utf-8 -*-  
  3. from math import pi  
  4. PI = [str(round(pi, i)) for i in range(110)]  
  5. print PI  
  6.   
  7.               
Output: 

    ['3.1', '3.14', '3.142', '3.1416', '3.14159', '3.141593', '3.1415927', '3.14159265', '3.141592654']

Example 6:

In this example you will see matrix transpose.

  1. matrix = [  
  2. [1234],  
  3. [5678],  
  4. [9101112],  
  5. ]  
  6. print(matrix)  
  7. transpose_mat1=[[row[i] for row in matrix] for i in range(4)]  
  8. print(transpose_mat1)  
  9. transposed_mat2 = []  
  10. for i in range(4):  
  11. # the following 3 lines implement the nested listcomp  
  12. rows = []  
  13. for row in matrix:  
  14. rows.append(row[i])  
  15. transposed_mat2.append(rows)  
  16. print(transposed_mat2)  
  17.               
Output: 

    [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
    [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
    [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

Stack

Stack is work on (“last-in, first-out”) concept. In List, the methods can be easily used for making stack concept on List. Add an item to the top of the stack, use append(). To retrieve an item from the top of the stack, use pop() without an explicit index.

In Stack we’ll use the following two methods:

  1. append()
  2. pop()

Example:

  1. #!/usr/bin/python  
  2. # -*- coding: utf-8 -*-  
  3. stack = [123445]  
  4.   
  5. # Add an item to the top of the stack  
  6.   
  7. stack.append(23)  
  8. print ('stack.append(23)=', stack)  
  9.   
  10. # Add an item to the top of the stack  
  11.   
  12. stack.append(73)  
  13. print ('stack.append(73)=', stack)  
  14.   
  15. # To retrieve an item from the top of the stack  
  16.   
  17. stack.pop()  
  18.   
  19. print ('stack.pop()=', stack)  
  20.   
  21. # To retrieve an item from the index 'X' of the stack  
  22.   
  23. stack.pop(1)  
  24. print ('stack.pop(1)=', stack)  
  25.   
  26. # To retrieve an item from the top of the stack  
  27.   
  28. stack.pop()  
  29. print ('stack.pop()=', stack)  
  30.   
  31.               
Output: 

    stack.append(23)= [12, 34, 45, 23]
    stack.append(73)= [12, 34, 45, 23, 73]
    stack.pop()= [12, 34, 45, 23]
    stack.pop(1)= [12, 45, 23]
    stack.pop()= [12, 45]

Queue

Queue is work on (“first-in, first-out”) concept. List are not efficient for this purpose.Becuase pop() method retrieve the top of the stack value.In Queue we always retrieve data from beginning of the list. To implement a queue in list we use collections.deque which are used for pop data from the beginning of the list.

Example:
  1. #!/usr/bin/python  
  2.   
  3. #- * -coding: utf - 8 - * -  
  4.     from collections  
  5. import deque  
  6. queue = deque(['Gmail''Yahoo''Outlook'])  
  7. print queue  
  8.   
  9. # Add an item in the queue  
  10.   
  11. queue.append('Rediff')  
  12. print('queue.append("Rediff") =', queue)  
  13.   
  14. # To retrieve an item from Beginning in the queue  
  15.   
  16. queue.popleft()  
  17. print('queue.popleft() =', queue)  
  18.   
  19. # To retrieve an item from Beginning in the queue  
  20.   
  21. queue.popleft()  
  22. print('queue.popleft() =', queue)  
Output:

    deque(['Gmail', 'Yahoo', 'Outlook'])
    queue.append("Rediff") = deque(['Gmail', 'Yahoo', 'Outlook', 'Rediff'])
    queue.popleft() = deque(['Yahoo', 'Outlook', 'Rediff'])
    queue.popleft() = deque(['Outlook', 'Rediff'])

Up Next
    Ebook Download
    View all
    Learn
    View all