Lists in Python can be combined in two ways.
Using the operator + Using extends. Let list_a and list_b be defined as shown below
list_a = [1, 2, 3, 4]
list_b = [5, 6, 7, 8]
Using the + operator to combine two list_a and list_b
list_c = list_a + lis_b
print (list_c)
Output: [1, 2, 3, 4, 5, 6, 7, 8]
If we do not want a new list, i.e. list_c
list_a = list_a + list_b
print (list_a)
# Output: [1, 2, 3, 4, 5, 6, 7, 8]
Using the extension
An extension operator can be used to combine one list into another. Unable to save the value in the third list. One of the existing lists should store the combined result.
list_c = list_a.extend (list_b)
print (list_c)
Output: NoneType
print (list_a)
Output: [1, 2, 3, 4, 5, 6, 7, 8]
print (list_b)
Output: [5, 6, 7, 8]
In the above example, extends combines list_b into list_a
Utthishsethuraman k
source share