Repository navigation
Expand file tree
/
Copy path058-Extend-List.py
More file actions
80 lines (64 loc) · 2.78 KB
/
Copy path058-Extend-List.py
File metadata and controls
80 lines (64 loc) · 2.78 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
"""
===============================================================================
File Name : 58-Extend-List.py
Description : Extend a List in Python
Author : Shaik Mahaboob Basha
===============================================================================
Definition:
The extend() method is used to add multiple elements from another iterable
(such as a list, tuple, or set) to the end of an existing list.
Syntax:
list_name.extend(iterable)
Example:
numbers.extend([40, 50, 60])
"""
# -----------------------------------------------------------------------------
# Creating the first list.
# -----------------------------------------------------------------------------
numbers = [10, 20, 30]
# -----------------------------------------------------------------------------
# Displaying the original list.
# -----------------------------------------------------------------------------
print("Original List :", numbers)
# Output:
# Original List : [10, 20, 30]
print()
# -----------------------------------------------------------------------------
# Creating another list.
# -----------------------------------------------------------------------------
new_numbers = [40, 50, 60]
# -----------------------------------------------------------------------------
# Displaying the second list.
# -----------------------------------------------------------------------------
print("New List :", new_numbers)
# Output:
# New List : [40, 50, 60]
print()
# -----------------------------------------------------------------------------
# Extending the first list with the second list.
# -----------------------------------------------------------------------------
numbers.extend(new_numbers)
# -----------------------------------------------------------------------------
# Displaying the updated list.
# -----------------------------------------------------------------------------
print("After Extending :", numbers)
# Output:
# After Extending : [10, 20, 30, 40, 50, 60]
print()
# -----------------------------------------------------------------------------
# Extending the list with another list of strings.
# -----------------------------------------------------------------------------
numbers.extend(["Python", "Java"])
# -----------------------------------------------------------------------------
# Displaying the updated list.
# -----------------------------------------------------------------------------
print("After Adding Strings :", numbers)
# Output:
# After Adding Strings : [10, 20, 30, 40, 50, 60, 'Python', 'Java']
print()
# -----------------------------------------------------------------------------
# Displaying the total number of elements.
# -----------------------------------------------------------------------------
print("Total Elements :", len(numbers))
# Output:
# Total Elements : 8