Repository navigation
Expand file tree
/
Copy path070-Remove-Elements-from-Tuple.py
More file actions
82 lines (67 loc) · 2.99 KB
/
Copy path070-Remove-Elements-from-Tuple.py
File metadata and controls
82 lines (67 loc) · 2.99 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
"""
===============================================================================
File Name : 70-Remove-Elements-from-Tuple.py
Description : Remove Elements from a Tuple in Python
Author : Shaik Mahaboob Basha
===============================================================================
Definition:
A tuple is immutable, which means its elements cannot be removed directly.
To remove elements from a tuple, first convert it into a list, remove the
required elements, and then convert it back into a tuple.
Syntax:
list_name = list(tuple_name)
list_name.remove(element)
tuple_name = tuple(list_name)
Example:
numbers = (10, 20, 30)
number_list = list(numbers)
number_list.remove(20)
numbers = tuple(number_list)
"""
# -----------------------------------------------------------------------------
# Creating a tuple of integers.
# -----------------------------------------------------------------------------
numbers = (10, 20, 30, 40, 50)
# -----------------------------------------------------------------------------
# Displaying the original tuple.
# -----------------------------------------------------------------------------
print("Original Tuple :", numbers)
# Output:
# Original Tuple : (10, 20, 30, 40, 50)
print()
# -----------------------------------------------------------------------------
# Converting the tuple into a list.
# -----------------------------------------------------------------------------
number_list = list(numbers)
# -----------------------------------------------------------------------------
# Removing the element 30 from the list.
# -----------------------------------------------------------------------------
number_list.remove(30)
# -----------------------------------------------------------------------------
# Removing the element 50 from the list.
# -----------------------------------------------------------------------------
number_list.remove(50)
# -----------------------------------------------------------------------------
# Converting the modified list back into a tuple.
# -----------------------------------------------------------------------------
numbers = tuple(number_list)
# -----------------------------------------------------------------------------
# Displaying the updated tuple.
# -----------------------------------------------------------------------------
print("Tuple After Removing Elements :", numbers)
# Output:
# Tuple After Removing Elements : (10, 20, 40)
print()
# -----------------------------------------------------------------------------
# Displaying the total number of elements in the updated tuple.
# -----------------------------------------------------------------------------
print("Total Elements :", len(numbers))
# Output:
# Total Elements : 3
print()
# -----------------------------------------------------------------------------
# Displaying the type of the updated tuple.
# -----------------------------------------------------------------------------
print("Type of Tuple :", type(numbers))
# Output:
# Type of Tuple : <class 'tuple'>