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:
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:
-
-
- a = [
- 1,
- 4,
- 3,
- 6,
- 3,
- 5,
- 6,
- 74,
- 6,
- 4,
- 3,
- 6,
- 2,
- 8,
- ]
-
-
-
- print (
- 'a.count(3)=',
- a.count(3),
- 'a.count(6)=',
- a.count(6),
- "a.count('x')=",
- a.count('x'),
- )
-
-
-
- a.insert(2, -1)
- print ('a.insert(2, -1)=', a)
-
-
-
- a.append(3)
- print ('a.append(3)=', a)
-
-
-
- print ('a.index(3)=', a.index(3))
-
-
-
- a.remove(3)
- print ('a.remove(3)=', a)
-
-
-
- a.reverse()
- print ('a.reverse()=', a)
-
-
-
- a.sort()
- print ('a.sort()=', a)
-
-
-
- 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)
- square_list = [x**2 for x in range(15)]
-
- 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’:
-
-
- 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):
- 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:
-
-
- stack = [12, 34, 45]
-
-
-
- stack.append(23)
- print ('stack.append(23)=', stack)
-
-
-
- stack.append(73)
- print ('stack.append(73)=', stack)
-
-
-
- stack.pop()
-
- print ('stack.pop()=', stack)
-
-
-
- stack.pop(1)
- print ('stack.pop(1)=', 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
The queue works on the (“first-in, first-out”) concept. The list is not efficient for this purpose. Because pop() method retrieves the top of the stack value. In Queue we always retrieve data from the beginning of the list. To implement a queue in the list we use collections.deque which is used for pop data from the beginning of the list.
Example:
-
-
-
- from collections
- import deque
- queue = deque(['Gmail', 'Yahoo', 'Outlook'])
- print queue
-
-
-
- queue.append('Rediff')
- print('queue.append("Rediff") =', queue)
-
-
-
- queue.popleft()
- print('queue.popleft() =', 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'])