Today, I took a new part of python. In this Part, we will see the usage of the list and how can use multiple ways for arranging data on the list. All the information and points are interesting in this part of the tutorial.
I have already told you.
Download Python Language Tutorials and give star on GitHub if those 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 much data. In Python, List has many functions 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 on 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:
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- a = [
- 1,
- 4,
- 3,
- 6,
- 3,
- 5,
- 6,
- 74,
- 6,
- 4,
- 3,
- 6,
- 2,
- 8,
- ]
- # Number of times values appear on the list.
- print (
- 'a.count(3)=',
- a.count(3),
- 'a.count(6)=',
- a.count(6),
- "a.count('x')=",
- a.count('x'),
- )
- # Insert an item at a given position
- a.insert(2, -1)
- print ('a.insert(2, -1)=', a)
- # Add an item to the end of the list
- a.append(3)
- print ('a.append(3)=', a)
- # the index in the list of the first item. It is an error if there is no such item
- print ('a.index(3)=', a.index(3))
- # Remove the first item from the list... It is an error if there is no such item
- a.remove(3)
- print ('a.remove(3)=', a)
- # Reverse the elements of the list
- a.reverse()
- print ('a.reverse()=', a)
- # Sort the items of the list
- a.sort()
- print ('a.sort()=', a)
- # removes and returns the last item in the list.
- print ('a.pop()=', a.pop())
-
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:
- square=[(x, x**2) for x in range(6)]
- #print square of each value
- print(square)
- square_list = [x**2 for x in range(15)]
- #print square of each value
- 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.
- comp_list = [(x, y) for x in [1, 2, 3]
- for y in [4, 5, 6]
- if x != y
- ]
- print(comp_list)
- combs = []
- for x in [1, 2, 3, ]:
- for y in [4, 5, 6]:
- if x != y:
- a.combs.append((x, y))
- print(combs)
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 elements in List.
- vec = [[1,2,3], [4,5,6], [7,8,9]]
- elem=[num for elem in vec for num in elem]
- print(elem)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Example 5:
In this example you will see use of ‘pi’:
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- from math import pi
- PI = [str(round(pi, i)) for i in range(1, 10)]
- print PI
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.
- matrix = [
- [1, 2, 3, 4],
- [5, 6, 7, 8],
- [9, 10, 11, 12],
- ]
- print(matrix)
- transpose_mat1 = [
- [row[i]
- for row in matrix
- ]
- for i in range(4)
- ]
- print(transpose_mat1)
- transposed_mat2 = []
- for i in range(4): #the following 3 lines implement the nested listcomp
- rows = []
- for row in matrix:
- rows.append(row[i])
- transposed_mat2.append(rows)
- print(transposed_mat2)
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 works on the (“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:
- append()
- pop()
Example:
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- stack = [12, 34, 45]
- # Add an item to the top of the stack
- stack.append(23)
- print ('stack.append(23)=', stack)
- # Add an item to the top of the stack
- stack.append(73)
- print ('stack.append(73)=', stack)
- # To retrieve an item from the top of the stack
- stack.pop()
- print ('stack.pop()=', stack)
- # To retrieve an item from the index 'X' of the stack
- stack.pop(1)
- print ('stack.pop(1)=', stack)
- # To retrieve an item from the top of the stack
- stack.pop()
- print ('stack.pop()=', stack)
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
- #!/usr/bin/python
- #- * -coding: utf - 8 - * -
- from collections
- import deque
- queue = deque(['Gmail', 'Yahoo', 'Outlook'])
- print queue
- # Add an item in the queue
- queue.append('Rediff')
- print('queue.append("Rediff") =', queue)
- # To retrieve an item from Beginning in the queue
- queue.popleft()
- print('queue.popleft() =', queue)
- # To retrieve an item from Beginning in the queue
- queue.popleft()
- 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'])

Santhakumar MunuswamyPosted Oct 6, 2015, 12:01 AM
Nice Share
Neeraj KumarPosted Oct 5, 2015, 8:59 AM
thank you Ankit Bansal
Ankit BansalPosted Oct 5, 2015, 5:58 AM
Nice..thanks for sharing with us..
Neeraj KumarPosted Oct 4, 2015, 5:11 PM
thank you Abhishek Jaiswal :)
Abhishek JaiswalPosted Oct 4, 2015, 1:09 PM
Good work buddy! :)
Neeraj KumarPosted Oct 4, 2015, 6:11 AM
thank you Rakesh Chavda
RakeshPosted Oct 4, 2015, 2:23 AM
Good Share buddy
Neeraj KumarPosted Oct 3, 2015, 11:44 PM
thank you Nilesh Jadav
Neeraj KumarPosted Oct 3, 2015, 11:44 PM
thank you Pankaj Kumar Choudhary
Neeraj KumarPosted Oct 3, 2015, 11:43 PM
thank you Sumit Joshi
Nilesh JadavPosted Oct 3, 2015, 9:28 PM
Nice one sir
Pankaj Kumar ChoudharyPosted Oct 3, 2015, 8:21 PM
Nice Explain Neeraj...........
Sumit JoshiPosted Oct 3, 2015, 4:19 PM
Thanks for sharing