The most common operations that we can perform on lists include joining lists, replicating lists and slicing lists.
The concatenation operator ‘+’, when used with two lists, joins the two lists. The + operator when used with the lists requires that both operands must be of list types.
a=[1, 2, 3, 4] b=[5, 6, 7, 8] c=a+b print(c)
Output- [1, 2, 3, 4, 5, 6, 7, 8]
We use * operator to replicate a list specified number of times i.e., concatenates multiple copies of the same list.
a=[1, 2, 3, 4] print(a* 3)
Slicing the lists:-
Returns the item at given index in a list. Fetches items in a range specified by the two index operands separated by the colon [:] symbol.
If the first operand is omitted, the range starts at zero index. If the second operand is omitted, the range goes up to the end of the list.
a=[1,2,3,4,5,6,7,8,9] print(a[3:6]) print(a[:7]) print(a[4:])
Output- [4,5,6] [1,2,3,4,5,6,7] [5,6,7,8]