Programming with Python

  1. Home
  2. Docs
  3. Programming with Python
  4. Lists in python
  5. Operations on lists in python

Operations on lists in python

The most common operations that we can perform on lists include joining lists, replicating lists and slicing lists.

Joining 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.

e.g.,

a=[1, 2, 3, 4]
b=[5, 6, 7, 8]
c=a+b
print(c)
Output- [1, 2, 3, 4, 5, 6, 7, 8]

Replicating lists:-

We use * operator to replicate a list specified number of times i.e., concatenates multiple copies of the same list.

e.g.,

a=[1, 2, 3, 4]
print(a* 3)
Output- [1,2,3,4,1,2,3,4,1,2,3,4]

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.

e.g.,

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]

Was this article helpful to you? Yes No

How can we help?