Python List Basics

By Titu Nahak 29 May, 2018
python list basics
Image from Pixabay

Python list is a sequence of objects in an ordered fashion. It is similar to arrays in other programming languages but it is heterogeneous in nature. A single list can hold different types of data objects(string, integer, float etc.). It can hold other lists also, a list inside another list is called nested list.

Note: You can run examples in this article on trinket.io online.

Create an empty list

>>> list1 = []
>>> print list1
[]          #output

Create a list with three elements

>>> list1 = [1, "hackinbits.com", 2.3]

Accessing a list element

>>> list1 = [1, "hackinbits.com", 2.3]
>>> list1[1]
'hackinbits.com'          #output

Updating a list

>>> list1 = [1, 2, 3, 4]
>>> list1[1] = 34
>>> print list1
[1, 34, 3, 4]          #output

An empty list can also be created using list() method

>>> list1 = list()
>>> print list1
[]          #output

In-built List Methods

There are various operations that can be performed on lists using in-build list methods.

append()

Add a new element to the end of the list.

>>> list1 = [1, 2]
>>> print list1
[1, 2]          #output

>>> list1.append(3)
>>> list1.append(4)
>>> print list1
[1, 2, 3, 4]          #output

insert()

Insert a new element at the specified index.

>>> list1 = [1, 2, 3]
>>> list1.insert(23, 1)
>>> print list1
[1, 23, 2, 3]          #output

remove()

Deletes the specified element from the list.

>>> list1 = [1, 2, 3]
>>> list1.remove(2)
[1, 3]          #output

pop()

Removes an item from the list and also returns it.

>>> list1 = [1, 2, 3]
>>> item = list1.pop()
>>> print list1
[1, 2]          #output

>>> print item
3          #output

index()

Returns the index of an element that matches first.

>>> list1 = [1, 2, 3, 4]
>>> list1.index(3)
2          #output

count()

Returns the total number of item count, passed as an argument to the method.

>>> list1 = [1, 2, 3, 4, 5, 3]
>>> list1.count(3)
2          #output

reverse()

Inverts the order of the elements in a list.

>>> list1 = [1, 2, 3]
>>> list1.reverse()
[3, 2, 1]          #output

extend()

Extends a list by adding elements from another list (passed as parameter).

>>> list1 = [1, 2]
>>> list2 = [3, 4]
>>> list1.extend(list2)
>>> print list1
[1, 2, 3, 4]          #output

>>> print list2
[3, 4]          #output

sort()

Orders the elements of a list in an ascending manner.

>>> list1 = [5, 6,3, 90, 3, 2]
>>> list1.sort()
>>> print list1
[2, 3, 3, 5, 6, 90]          #output

List copy in python

A list copy operation can be performed using assignment operator.

>>> list1 = [1, 2, 3, 4]
>>> list2 = list1
>>> print list2
[1, 2, 3, 4]          #output

This kind of copy using assignment operator is called shallow copy. Any changes in list1 will make the same change in list2.

>>> list1[2] = 22
>>> print list2
[1, 2, 22, 4]          #output

Solution to shallow copy in python list:

  1. Using in built copy module

    >>> import copy
    >>> list1 = [1, 2, 3, 4]
    >>> list2 = copy.deepcopy(list1)
    >>> print list2
    [1, 2, 3, 4]          #output

    >>> list1[1] = 22
    >>> print list2
    [1, 2, 3, 4]          #output

  2. Creating another list and copying the content

    >>> list1 = [1, 2, 3, 4]
    >>> list2 = []
    >>> for i in list1:
                list2.append(i)
    >>> list1[1] = 22
    >>> print list2
    [1, 2, 3, 4]          #output

Follow us on
Sponsored