Understanding lists in Python

A guide to learn about python lists in detail

Jayashree domala
3 min readSep 3, 2020

Lists are an ordered sequence that can hold a multitude and variety of object types. They are represented by [] and the objects are written inside of it which are comma separated.

For example: [4, ‘jay’, ‘6’]

Lists support indexing and slicing. The can also be nested and have a number of useful methods to implement them accordingly.

>>> list1 = [4,6,2,1]
>>> list2 = [3, 'jay', 98.5]
>>> len(list2)
3

Indexing

>>> list3 = ['python', 'is', 'fun']
>>> list3[0]
'python'
>>> list3[-1]
'fun'

It should also be noted that lists indexing will return an error if there is no element at that index.

>>> list3[10]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-49-2d72a649ac7f> in <module>
----> 1 list3[10]
IndexError: list index out of range

Slicing

>>> list3[2: ]
['fun']
>>> list3[:]
['python', 'is', 'fun']

Concatenation

>>> list4 = list2 + list3
>>> list4
[3, 'jay', 98.5, 'python', 'is', 'fun']

Mutability

>>> list3
['python', 'is', 'fun']
>>> list3[0] = "Java"
>>> list3
['Java', 'is', 'fun']

Add items to list

>>> list3.append('easy')
>>> list3
['Java', 'is', 'fun', 'easy']

Remove items from list

The default popped index is -1.

>>> list3.pop()
'easy'
>>> list3
['Java', 'is', 'fun']

Removing an item at a specific position

>>> list3.pop(1)
'is'
>>> list3
['Java', 'fun']

Sort

Sort in ascending order for numbers and alphabetical order for alphabets

>>> alpha = ['f', 'e', 'h', 'a']
>>> num = [5,23,8,1,77]
>>> alpha.sort()
>>> alpha
['a', 'e', 'f', 'h']
>>> num.sort()
>>> num
[1, 5, 8, 23, 77]

This is a special inplace method. It doesn't return anything and sorts in place. Therefore when you give it a variable and call it, no output is obtained. The type when checked returns “NoneType”. NoneType is the type for a “None” object. “None” is a special object in python which is used to inidcate no value.

>>> num1 = [54,24,8,12,77]
>>> sorted_num1 = num1.sort()
>>> sorted_num1
>>> type(sorted_num1)
NoneType

So if we need to assign the sorted list to the variable we should do the following

>>> num1.sort()
>>> sorted_num1 = num1
>>> sorted_num1
[8, 12, 24, 54, 77]

Reverse

Even this is an inplace method.

>>> num1.reverse()
>>> num1
[77, 54, 24, 12, 8]

Duplication

>>> num1 * 2
[77, 54, 24, 12, 8, 77, 54, 24, 12, 8]

Nested lists

A great feature of of Python data structures is that they support nesting. This means we can have data structures within data structures. For example: A list inside a list.

>>> lst1=[11,22,33]
>>> lst2=[44,55,66]
>>> lst3=[77,88,99]
>>> nested_lst = [lst1,lst2,lst3]
>>> nested_lst
[[11, 22, 33], [44, 55, 66], [77, 88, 99]]

We can again use indexing to grab elements, but now there are two levels for the index. The items in the nested object, and then the items inside that list.

Grab first item in matrix object

>>> nested_lst[0]
[11, 22, 33]

Grab first item of the first item in the nested object

>>> nested_lst[0][0]
11

List comprehensions

Python has an advanced feature called list comprehensions. They allow for quick construction of lists. We will use a list comprehension to grab the first element of every row in the nested object.

Using for loop to build a list comprehension

>>> col1 = [row[0] for row in nested_lst]
>>> col1
[11, 44, 77]

Refer to the notebook here.

Beginner-level books to refer to learn Python:

Advance-level books to refer to learn Python:

Reach out to me: LinkedIn

Check out my other work: GitHub

--

--

Jayashree domala
Jayashree domala

Written by Jayashree domala

Self-driven woman who wishes to deliver creative and engaging ideas and solutions in the field of technology.

Responses (1)