In this blog we will look at various functions of list how to acess, add elements, removing etc. List is like a array ypu studied in some other programming languages may be. In Python it is mutable.

Mutable and immutable concept is been discussed during string data type. Hope you are clear with that.

Let’s get started to check different methods of it.

How to create list and accessing it.

In [1]:

l1 = [1,2,3,4]
print(l1)

[1, 2, 3, 4]

In [2]:

print(l1[0])
print(l1[1])

1
2

Slicing method to access the elements

In [3]:

## It will not include the end index
print(l1[0:3])

## Accesssing full list
print(l1[:])

## With skip also ## here it will skip 1 value and then takes another.
## Same as for(i=0;i<n;i=1+2) if you have done c or c++, java programmin before. print(l1[0::2])

## Access from last
 print(l1[:-2])

[1, 2, 3]
[1, 2, 3, 4]
[1, 3]
[1, 2]

Lists are mutable

Mutable – See here, on changing value of l1 at index 0, l2 also gets modified as it is pointing to same memory location as l1.2In [4]:

l2 = l1
l1[0] = 0

print(l1)
print(l2)

[0, 2, 3, 4]
[0, 2, 3, 4]

Lists are heterogenous

All elements of lists is not necessary to be of same data type.In [5]:

l3 = ['a' ,1, 'hello']
print(l3)

['a', 1, 'hello']

Methods of list

1. len()

It will return how many elements are there in listIn [6]:

print(len(l3))

3

2. in

Actually it is an operator basically, which will check if this element is present in list or not.In [7]:

if 'a' in l3:
     print('Yes, this element is in the list')

Yes, this element is in the list

“in” can also be used for printing values in a loopIn [8]:

for e in l3:
     print(e)

a
1
hello

3. index(element, beg, end)

It will return the index of the searched element. It takes 3 parameters.

  1. element -> element you want to search.
  2. beg -> from which index you want to start searching.
  3. end -> till which index you want to start searching.

beg and end parameters are optional.In [9]:

## Checking the index of '1'
l3.index(1)

Out[9]:

1

In [10]:

## Here we start searching beyond the index in which '1' lies so it throws ValueError
l3.index(1,2)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
 in ()
      1 ## Here we start searching beyond the index in which '1' lies so it throws ValueError
----> 2 l3.index(1,2)

ValueError: 1 is not in list

4. count()

It will count the occurence of the provided no. of valueIn [11]:

l4 = [1,2,3,2,3,4,5,6]
l4.count(3)

Out[11]:

2

How to add elements in list?

5. append()

It will append that element in the end of the listIn [12]:

## It added one more new element '7' in the last.
l4.append(7)
print(l4)

[1, 2, 3, 2, 3, 4, 5, 6, 7]

In [13]:

## We can also add sublist as an element in the original list as we saw earlier, list is ## heterogenpus in nature.
l4.append([1,2])
print(l4)

[1, 2, 3, 2, 3, 4, 5, 6, 7, [1, 2]]

6. insert(postion, element)

It will insert the element at the given position. It takes 2 parameters -:

  1. position -> at which position you wants to insert. It is not optional.
  2. element -> element to be inserted.

In [14]:

l4.insert(5,1)
## See it shifted elemnts from index 5 to right and inserted 1 at index 5.
print(l4)

[1, 2, 3, 2, 3, 1, 4, 5, 6, 7, [1, 2]]

7. extend(list)

This function is used to extend the list with the elements present in another list. This function takes another list as its argument.In [15]:

## Here we appended the elements present in the l3 to l4.
l4.extend(l3)
print(l4)

[1, 2, 3, 2, 3, 1, 4, 5, 6, 7, [1, 2], 'a', 1, 'hello']

In [16]:

## It will remain the same.
print(l3)

['a', 1, 'hello']

How to delete elements from list?

8. pop(n)

It will pop the element from that index. It is optional argument, if not mentioned that it will pop from end.In [17]:

l4.pop(5)
print(l4)

[1, 2, 3, 2, 3, 4, 5, 6, 7, [1, 2], 'a', 1, 'hello']

In [18]:

l4.pop()
print(l4)

[1, 2, 3, 2, 3, 4, 5, 6, 7, [1, 2], 'a', 1]

9 del[a:b]

It will delete the elements in a range, excluding the end index.In [19]:

del l4[3:5]
print(l4)

[1, 2, 3, 4, 5, 6, 7, [1, 2], 'a', 1]

10. remove()

This function is used to delete the first occurrence of number mentioned in its arguments.In [20]:

l4.remove(1)
print(l4)

[2, 3, 4, 5, 6, 7, [1, 2], 'a', 1]

11. clear()

It will make the whole list empty.In [21]:

l2.clear()
print(l2)

[]

How to sort and reverse the list?

11. sort() and sorted()

It will sort the list in increasing order, only for integers not string.

There is a difference between sort() and sorted() , sort will make changes in the exact location/memory but sorted() will sort only for that time and will not replicate in memory.

In sort we have one more parameter, reverse. If reverse = True, then it will sort in descending order.In [22]:

l5 = [4,5,1,2,8,9,1]

## It will sort the list in ascending order.
l5.sort()
print(l5)

[1, 1, 2, 4, 5, 8, 9]

In [23]:

l5 = [4,5,1,2,8,9,1]

## It will sort in reverse order i.e, descending 
l5.sort(reverse= True) print(l5)

[9, 8, 5, 4, 2, 1, 1]

In [24]:

l6 = [4,5,1,2,8,9,1]

## using sorted it will sort for that time only.
sorted(l6)

Out[24]:

[1, 1, 2, 4, 5, 8, 9]

In [25]:

## If we will print the list it will same only
print(l6)

[4, 5, 1, 2, 8, 9, 1]

12. reverse()

It will reverse the whole listIn [26]:

## Reversing the list
l6.reverse()

In [27]:

l6

Out[27]:

[1, 9, 8, 2, 1, 5, 4]