Repository navigation
Expand file tree
/
Copy path069-Convert-Tuple-to-List.py
More file actions
75 lines (61 loc) · 2.66 KB
/
Copy path069-Convert-Tuple-to-List.py
File metadata and controls
75 lines (61 loc) · 2.66 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
"""
===============================================================================
File Name : 69-Convert-Tuple-to-List.py
Description : Convert a Tuple to a List in Python
Author : Shaik Mahaboob Basha
===============================================================================
Definition:
A tuple can be converted into a list using the list() function. This is useful
when you want to modify the elements because tuples are immutable, whereas
lists are mutable.
Syntax:
list_name = list(tuple_name)
Example:
numbers = (10, 20, 30)
number_list = list(numbers)
"""
# -----------------------------------------------------------------------------
# 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)
# -----------------------------------------------------------------------------
# Displaying the converted list.
# -----------------------------------------------------------------------------
print("Converted List :", number_list)
# Output:
# Converted List : [10, 20, 30, 40, 50]
print()
# -----------------------------------------------------------------------------
# Adding a new element to the list.
# -----------------------------------------------------------------------------
number_list.append(60)
# -----------------------------------------------------------------------------
# Displaying the updated list.
# -----------------------------------------------------------------------------
print("List After Adding 60 :", number_list)
# Output:
# List After Adding 60 : [10, 20, 30, 40, 50, 60]
print()
# -----------------------------------------------------------------------------
# Displaying the type of the original tuple.
# -----------------------------------------------------------------------------
print("Type of Tuple :", type(numbers))
# Output:
# Type of Tuple : <class 'tuple'>
# -----------------------------------------------------------------------------
# Displaying the type of the converted list.
# -----------------------------------------------------------------------------
print("Type of List :", type(number_list))
# Output:
# Type of List : <class 'list'>