Different Operations on List in Python with examples

Different Operations on List in Python with examples

3 min read

Accessing List Elements

Similar to strings, items are stored into a list according to their index. Any item can be accessed using its index value.

  • The list index can be either positive or negative integer.
  • The positive index starts from 0 and negative starts from -1.

For example:-

#Each element can be accessed by its index value
>>>list=[11,22,33,44,55,66,77,88,99]
>>>list1[0]
11
#Negative values can also be used here
>>>list1[-2] # Here -2 will be treated as length of list-2
88
>>>list1[-5]
55

Traversing a list

Traversing means to travel across a list or to access all of its elements(items).

There are two ways to traverse a list in Python:

1. Using ‘in’ operator

  • ‘in’ operator is used to check whether the element is present in the list or not.
  • If you want to check the character is present in the list or not you can use this operator. It is useful in that case.
  • It can be used in for loop as iterator on the list. As shown in the below example.
>>>list1=['F','R','E','S','H','L','Y','B','U','I','L','T']
>>>for i in list1:
>>>    print(i)  #Here i is the iterator
F
R
E
S
H
L
Y
B
U
I
L
T

2.Using range() function

  • range() function in python is used for traversing through the list.
  • The numerical value or range value is passed inside the function for providing the traversing limit.
  • It can also be used for traversing the list by leaving the element inside the list in a fixed manner.
  • For example, is we have to traverse over ‘ram’ we can also traverse over ‘ram’ by iterating over ‘r’ and ‘m’ and leaving a behind i.e range(3,1) # 3 equal to the length of the word and 1 signify that we have to leave single character after taking out one character while traversing over the list.
>>>list1=['F','R','E','S','H','L','Y','B','U','I','L','T']
>>>n=len(list1) #here n is length of the list
>>>for i in range(n):
>>>    print(list1[i])  #Here i is the iterator
F
R
E
S
H
L
Y
B
U
I
L
T

Comparison in Lists

In python, if lists are of comparable types then you can compare each element individually in lexicographical(dictionary) order. You can apply all relational operators to compare elements in the lists.

>>> [1,3,5] < [2,1,3] # Here 1 is less than 2
True
>>> [1,3,2]<[1,2,4] # Here 3 is not less than 2
False

List Concatenation

Similar to strings, lists also can be combined together. The ‘+’ operator(or the concatenation operator) can be used to join two lists and create a new list

>>> l1=[2,5,7,3]
>>> l2=[3,6,4,1]
>>>print(l1+l2) # Here both l1 and l2 will be merged to create a new list.
[2,5,7,3,3,6,4,1]
>>>l1=["Red","Yellow"]
>>>l1=l1+["Green"]
>>>print(l1)
["Red","Yellow","Green"]
>>>list=[2,3,5]
>>>list=list+43
Traceback (most recent call last):
   File "<stdin>",line 1, in <module>
TypeError: can only concatenate list (not "int") to list
>>># Here concatenation can be done between two lists only.

Replication

Replication means to make a replica(copy). It is used to replicate a list for a specified number of times.

The list should always be multiplied by an integer. Otherwise python will raise an error.
>>>l=[8, 16, 24, 32, 40]
>>>print(l*2)
[8, 16, 24, 32, 40, 8, 16, 24, 32, 40]
Tags:
Choose your Reaction!
Leave a Comment