You are currently viewing Lists in Python – Everything you need to know as a programmer

Lists in Python – Everything you need to know as a programmer

  • Post author:
  • Post category:Python

Lists are used to store multiple items in a single variable. We can store different data types in a list. List items are ordered, changeable, and allow duplicate values. Indexing start from 0 to n-1.

Creating a List in Python

Lists are created using square brackets.

empty_list = []
my_list = [2,"the",4.5,True]

print(empty_list) # []
print(my_list) # [2,"the",4.5,True]

Time Complexity: O(1)

Space Complexity: O(n)

Take List Input

We can take input list from the user. The input is converted into list by using split() method. By default the input is of string data type 

user_input = input() # user enters input eg. hey 3 True
input_list = user_input.split() 
print(input_list) # ['hey' , '3' , 'True']

Getting the length of a list

To get the number of elements in an array, we can use the built-in len() method.

my_list = [2,"the",4.5,True]
print(len(my_list)) # 4

Access Elements of a List

To access the elements of the list, we specify the index to get the element at the index.

my_list = [2,"the",4.5,True]

print(my_list[1]) # the
print(my_list[2]) # 4.5

Negative indexing

In a list, negative indexes represent positions from the end. -1 refers to the last item, -2 refers to the second-last item, and do on.

my_list = [2,"the",4.5,True]

print(my_list[-1]) # True
print(my_list[-4]) # 2

Time Complexity: O(1)

Space Complexity: O(1)

Search Elements in a List

To search for the index of an element in a list, we will use the index().

my_list = [2,"the",4.5,True]

print(my_list.index("the")) # 1

Time Complexity: O(n) since we have to iterate through the list

Space Complexity: O(1)

Slicing of a List

To access a specific range of values inside the array, we use the slicing operator  : .

We specify the start index and the end index, separated by a colon. The end index element is not included.

my_list = [2,"the",4.5,True,"she",3,"coder",False]

print(my_list[2:5]) # [4.5, True, 'she']

By leaving out the start index, we can get the characters from the start to the position specified (not included in the output).

my_list = [2,"the",4.5,True,"she",3,"coder",False]

print(my_list[:5]) # [2, 'the', 4.5, True, 'she']

By leaving out the end index, we can get the characters from the specified position , to all the way till the end.

my_list = [2,"the",4.5,True,"she",3,"coder",False]

print(my_list[2:]) # [4.5, True, 'she', 3, 'coder', False]


Change the value at specified index in a List

We can change the value at specific index by specifying the index and assigning it a new value

my_list = [2,"the",4.5,True,"she",3,"coder",False]

my_list[0] = 8 
print(my_list) # [8, 'the', 4.5, True, 'she', 3, 'coder', False]

Adding Elements to a Python List

There are three ways to add element to a List

1) insert(index, element) – used to add a new element at any given index of the array. The index and the element is specified in its arguments. Since we have to move other elements in the array to insert, the Time Complexity: O(n) and Space: O(1).

my_list = [2,"the",4.5,True,"she",3,"coder",False]
my_list.insert(1,"hey")
print((my_list)) # [2, 'hey', 'the', 4.5, True, 'she', 3, 'coder', False]

2) append() – used to add a new element at the end of the array. Since we are adding at the end and not moving other elements in the array the Time Complexity: O(1) and Space Complexity : O(1).

my_list = [2,"the",4.5,True,"she",3,"coder",False]
my_list.append("hey")
print((my_list)) # [2, 'the', 4.5, True, 'she', 3, 'coder', False, 'hey']

3) extend() –  used to add multiple elements at the same time at the end of the list. The Time Complexity: O(n) and Space Complexity: O(1).

my_list = [2,"the",4.5,True,"she",3,"coder",False]
my_list.extend(["hey",4])
print((my_list)) #[2, 'the', 4.5, True, 'she', 3, 'coder', False, 'hey', 4]

Removing Elements from a List

There are two ways to remove elements from an array

1)  remove(value) – only the first occurrence of the value passed in the argument is removed.

my_list = [2,3,4,2,5]
my_list.remove(2)
print((my_list)) # [3,4,2,5]

2) pop() – by default remove the last element of the array. To remove element from a specific position in the array, the index of the element is passed as an argument.

my_list = [2,3,4,2,5]

my_list.pop() # pops last element
print(my_list) # [2,3,4,2]

my_list.pop(2) # pops element at index 2
print(my_list) # [2,3,2]

The Time Complexity for both will be O(1) if we are removing elements at the end of the array and O(n) if we are removing elements at the any other position. The Space Complexity will be O(1).