Repository navigation
Expand file tree
/
Copy path062-Pop-Element-from-List.py
More file actions
84 lines (69 loc) · 2.89 KB
/
Copy path062-Pop-Element-from-List.py
File metadata and controls
84 lines (69 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
===============================================================================
File Name : 62-Pop-Element-from-List.py
Description : Pop an Element from a List in Python
Author : Shaik Mahaboob Basha
===============================================================================
Definition:
The pop() method is used to remove and return an element from a specified
index in a list. If no index is specified, it removes and returns the last
element.
Syntax:
list_name.pop(index)
or
list_name.pop()
Example:
numbers.pop(2)
numbers.pop()
"""
# -----------------------------------------------------------------------------
# Creating a list of integers.
# -----------------------------------------------------------------------------
numbers = [10, 20, 30, 40, 50]
# -----------------------------------------------------------------------------
# Displaying the original list.
# -----------------------------------------------------------------------------
print("Original List :", numbers)
# Output:
# Original List : [10, 20, 30, 40, 50]
print()
# -----------------------------------------------------------------------------
# Removing the last element from the list.
# -----------------------------------------------------------------------------
removed_element = numbers.pop()
# -----------------------------------------------------------------------------
# Displaying the removed element.
# -----------------------------------------------------------------------------
print("Removed Last Element :", removed_element)
# Output:
# Removed Last Element : 50
# -----------------------------------------------------------------------------
# Displaying the updated list.
# -----------------------------------------------------------------------------
print("List After pop() :", numbers)
# Output:
# List After pop() : [10, 20, 30, 40]
print()
# -----------------------------------------------------------------------------
# Removing the element at index 1.
# -----------------------------------------------------------------------------
removed_element = numbers.pop(1)
# -----------------------------------------------------------------------------
# Displaying the removed element.
# -----------------------------------------------------------------------------
print("Removed Element at Index 1 :", removed_element)
# Output:
# Removed Element at Index 1 : 20
# -----------------------------------------------------------------------------
# Displaying the updated list.
# -----------------------------------------------------------------------------
print("List After pop(1) :", numbers)
# Output:
# List After pop(1) : [10, 30, 40]
print()
# -----------------------------------------------------------------------------
# Displaying the total number of elements in the list.
# -----------------------------------------------------------------------------
print("Total Elements :", len(numbers))
# Output:
# Total Elements : 3