Skip to main content

Command Palette

Search for a command to run...

Lists in PYTHON

Published
•5 min read•View as Markdown
S

Someone who loves reading and writing

What are Lists in Python?

It is an ordered set of values between [ ] where each value is identified by an Index and value is known as an element in the list. It is a type of data structure supported by the Python language. The elements in the lists can be of mixed data types.

Using range ( ) to create a list

Python has an in-built function range( ) to create a list of consecutive integers. The function takes 3 arguments in total to create the list.

range ( ) with 1 argument

If the function is called with only 1 argument, then it creates a list of integers starting from ‘0’ (zero) and stops before the number given in the argument.

range ( ) with 2 arguments

When 2 arguments are passed in the function, then the 1st argument acts as the starting element of the list, and the 2nd argument acts as before which the list needs to be stopped.

range ( ) with 3 arguments

When 3 arguments are passed in the function, then 1st argument acts as the start element, 2nd argument as the number before which the list needs to be stopped, and 3rd argument as the gap between 2 successive elements in the list.

Below is the table with examples

Examples

Function with argumentsList
list(range (10))[0,1,2,3,4,5,6,7,8,9]
list(range(1, 5))[1,2,3,4]
list(range(1, 10, 2))[1,3,5,7,9]

Accessing elements from the LIST

The way to access an element from a list is by just passing the index of the element in the list within the [] brackets, after the variable name. The [] also takes an expression that will be evaluated to the index. If the Index is Invalid or it doesn’t exist in the list, it returns a runtime error.

Examples

» numbers [1,2,3,4,5]

» print numbers [2] = 3 // access element at Index 2 in the list “numbers“

If a -ve Index is passed under the brackets, the position or index is counted from the end of the list

Examples

» numbers [-1] = 5 // last element of the list

» numbers [-2] = 4 // 2nd last element of the list

Length of the List

The function len( ) returns the length of the list to which it is applied

Example: numbers.len() = 5 // total number of elements in the list “numbers“

Checking the membership in a LIST

It is a Boolean operator that checks for Inclusion or Membership of a particular value inside a particular list.

Examples:

» colors = [‘red‘, ‘blue’, ‘green’, ‘orange’, ‘pink‘]

» ‘red‘ in colors // True

» ‘black’ in colors // False

Lists Operations

  1. Concatenate Lists using ‘+’ operator

    The ‘+‘ operator is used to concatenate 2 lists

    Examples:

    » a = [1, 2, 3]
    » b = [4, 5, 6]

    c = a + b = [1, 2, 3, 4, 5, 6] // concatenate 2 lists into 1

  2. Repeat lists using the ‘*’ operator

    » a = [0]

    » b = a * 4

    » b = [0,0,0,0] // repeating the elements of the list 4 times within a single list

Slice Operator in Lists

The operator is denoted by square brackets [ ] with a colon (:) symbol. The operator forms a new list from the existing list.

Examples

» list = [‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ’8’, ‘9’ ]
» newlist = list[1 : 3] // slice the list starting from Index 1 and end till before 3
» newlist = [‘2’, ‘3’]

» list[ : 4] // start will be 1st element or zero Index
» list [3: ] // start from Index 3 up to the end or last element
» list [ : ] // start from 1st element upto last element. This creates a new list

Lists are Mutable

Lists are mutable, that means elements of a list can be changed or mutated. 1 element or more elements can be changed in a list.

Examples:

» colours = [“blue“, “pink“, “red“, “orange“]
» colors[0] = “green“ // 1st element was updated
» colors[-1] = “blue“ // -1 means last element
» print colours
» [“green“, “pink“, “red“, “blue“]

Mutating more than 1 element using the slice operator [:]

» list = [“a“, “b“, “c“, “d“, “e“, “f“, “g“, “h“]
» list [1:3] = [“x“, “y“] // Injecting 2 elements at position starting from Index 1 and stopping before Index 3
» print list
» [“a“, “x“, “y“, “b“, “c“, “d“, “e“, “f“, “g“, “h“]

Note: Elements are not removed from the list, but are shifted by the new elements.

Deleting elements from the Lists

» numbers = list(range(5))
» print numbers
» [0, 1, 2, 3, 4]
» numbers[1:3] = [ ] // assigning empty values at position starting from 1
» print numbers
» [ 0, 3, 4]

Cloning Lists

When a list is copied to another variable, without copying the reference is known as cloning.

» numbers = [1, 2, 3]
» integers = numbers[:] // the complete list is copied to ‘integers‘
» print integers
» [1, 2, 3]

Lists as in Parameters / Arguments

Whenever a list is passed as a parameter, the reference to the list is passed and not a copy of the list

» numbers = [1, 2, 3]
» newList = printList(numbers)
» print newList
» [1, 2, 3] // a new list which is separated from numbers and will have no impact on numbers

Nested Lists

It is a list that is an element in another list

» list = [“hello“, 2.0, [5, 2, 3]]

Access list from a list
» print list [2]
» [5, 2, 3]

Accessing an element from the list
1st way
» newList = list[2]
» ele = newList[0]
» print ele
» 5
2nd way
» ele = list[2][0]

Strings as List

A string can be split into a list.

» song = “rain rain come again another day“
» list = String.split(song)
» print list
» [“rain“, “rain“, “come“, “again“, “another“, “day“]

Disclaimer: This article is written as a revision note, and I will be updating it as I get to know more about Lists in Python. So, feel free to provide feedback with a pinch of salt. :)