04. Lists

Arindam BaidyaArindam Baidya
5 min read

Lists

  • Collection of multiple elements.

  • In a list, each elements is a scaler and each element in the list has an index

  • Index starting from 0

Accessing elements

>>> countries = ["USA", "Canada", "India"]
>>> print(countries[0])
USA
>>> print(countries[1])
Canada
>>> print(countries[2])
India

Modifying elements

>>> countries = ["USA", "Canada", "India"]
>>> countries[0] = "UK"

len() : Length of a list

>>> countries = ["USA", "Canada", "India"]
>>> len(countries)
3

del : Delete items

>>> countries = ["USA", "Canada", "India"]
>>> del countries[1]

Accessing values

>>> countries = ["USA", "Canada", "India"]
>>> print(countries[-1])
India

Lists - Methods

In order to change the data in lists, we can use built-in python methods.

  • Methods are specific kind of functions. They behave and look like a function, but the purpose is a bit different.

  • Whareas a function acts on its own. A method is owned by the data that it works for.

list.append() : Append new items at the end of the list

>>> countries = ["USA", "Canada", "India"]
>>> countries.append("Spain")

list.insert()

  • Insert new items, in between values instead of just at the end of the list.
>>> countries = ["USA", "Canada", "India"]
>>> countries.insert(2, "Italy")
  • The first argument that this method receives is the index that the new value should have.

Swapping values

>>> countries = ["USA", "Canada", "India"]
# swaping USA and CANADA
>>> temp = countries[0]
>>> countries[0] = countries[1]
>>> countries[1] = temp

An easier way to swap below:

>>> countries = ["USA", "Canada", "India"]
>>> countries[0], countries[1] = countries[1], countries[0]

list.sort()

  • To sort a list from lowest to highest value or from the lowest letter in the alphabet to the highest.

  • This method modifies the original array

>>> ages = [56, 72, 24, 46]
>>> ages.sort()
>>> print(ages)
[24, 46, 56, 72]

list.reverse()

  • Reverses all the elements in a list
>>> ages = [56, 72, 24, 46]
>>> ages.reverse()
>>> print(ages)
[46, 24, 72, 56]

Iterating lists

for loop

>>> ages = [56, 72, 24, 46]
>>> total = 0
>>> for age in ages:
        total += age
>>> average = total/len(ages)
>>> print(average)
49.5

Understanding Lists

There is one important difference when working with lists versus working with regular variables.

  • When we define a normal variable, python stores its value in memory directly.

  • When we create a list, however we are actually creating a variable which value is simply a reference to the address where the list is stored in memory.

  • Now, When ever we try to create a new variable, that should have the same value as the initial variable, We are actually copying the reference to the same spot in memory that the initial variable referes to. This means that whenever we changes the value of an element on any one of the list variable, in the other one it will also reflect.

  • If we don’t want this to happen, we can slice the list.

>>> name = "arindam"
>>> ages = [25, 78, 25, 98]
>>> ages2 = ages
>>> ages2[0] = 55
>>> print(ages[0])
55

Slicing lists

We can slice a list by using square brackets and specifying an optional start and end index that specifies how we want to slice that list.

list[start:end]

  • The element in the first index is the first element that should be included in the new list and the element in the end index won’t be included in this slice list, but it will be sliced exactly before that element.
>>> letters = ["A", "B", "C", "D", "E"]
>>> firstTwo = letters[0:2] # Slicing - First two elements
>>> print(firstTwo)
["A", "B"]
>>> print(letters[1:])
["B", "C", "D", "E"]
>>> print(letters[:3])
["A", "B", "C"]
  • We can specify negative indices, which is useful when we don’t exactly know the length of the list. Last element index: -1
>>> letters = ["A", "B", "C", "D", "E"]
>>> print(letters[1:-1])
["B", "C", "D"]

Slicing a list with all elements or copying a list into slice

>>> letters = ["A", "B", "C", "D", "E"]
>>> print(letters[:])
["A", "B", "C", "D", "E"]
  • If we now modify the list(slice), we are not modifying the original list.

Using del keyword to slicing a list - is meant to modifying the original array/list

>>> letters = ["A", "B", "C", "D", "E"]
>>> del letters[1:3]

Deleting all elements

>>> letters = ["A", "B", "C", "D", "E"]
>>> del letters[:]
>>> print(letters)
[]

Finding in lists

Python gives us an easy way to find an element is in the list or not.

  • element in list

  • element not in list

It returns boolean output.

>>> letters = ["A", "B", "C", "D", "E"]
>>> print("B" in letters)
True
>>> print("Z" in letters)
False
>>> letters = ["A", "B", "C", "D", "E"]
>>> print("B" not in letters)
False
>>> print("Z" not in letters)
True

Nested Lists - 2D

Nested lists - 3D

>>> student = school[1][2][1]
>>> print(student)
Max

References

Kodekloud

0
Subscribe to my newsletter

Read articles from Arindam Baidya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Arindam Baidya
Arindam Baidya