Concatenation of List using “+” Operator
We can also use + operator to combine two lists. This is also called concatenation.
n = [1, 3, 5]
print(n + [9, 7, 5])
# Output: [1, 3, 5, 9, 7, 5]
We can also use + operator to combine two lists. This is also called concatenation.
n=[1,2,3,4,5]
n1=[6,7,8,9,10]
print(“list n”)
print(n)
print(“list n1”)
print(n1)
print(“n+n1”)
print(n+n1)
#[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(n+[10,20,30])
output:
list n
[1, 2, 3, 4, 5]
list n1
[6, 7, 8, 9, 10]
n+n1
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 10, 20, 30]
Example:1
n1=[1,2,3,4,5] n2=[10,20,30,40,50] print(n1) print(n2) print(n1+n2) print(n2+n1)
Output:
[1, 2, 3, 4, 5]
[10, 20, 30, 40, 50]
[1, 2, 3, 4, 5, 10, 20, 30, 40, 50]
[10, 20, 30, 40, 50, 1, 2, 3, 4, 5]
Repeating or Replicating Lists
Operator “*” helps us to replicate a list specified number of times.
Example:
n=[1,2,3]
print(n)
print(n*2)
print(n*3)
Output:
[1, 2, 3]
[1, 2, 3, 1, 2, 3]
[1, 2, 3, 1, 2, 3, 1, 2, 3]
n=[1,2,3] print(n) print(n*2) print(n*3)
Output:
[1, 2, 3]
[1, 2, 3, 1, 2, 3]
[1, 2, 3, 1, 2, 3, 1, 2, 3]
Example:
n=[‘a’,’b’,’c’]
print(n)
print(n*2)
print(n*3)
Output:
[‘a’, ‘b’, ‘c’]
[‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘c’]
[‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘c’]
n=['a','b','c'] print(n) print(n*2) print(n*3)
Output:
[‘a’, ‘b’, ‘c’]
[‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘c’]
[‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘c’]