You are on page 1of 4

As Maria moved from one section to another in the shop, list kept on

changing.

In this case, we can use list data structure to represent Maria’s list of
grocery items.

List is

i. a linear data structure


ii. used to store a sequence of values
iii. can grow and shrink dynamically based on need

Common operations on list are:

Operation Description

add Add an element to the end of the list or append an element

insert Insert an element at a given position

delete Delete an element from a given position

display Display the elements of the list

List can be implemented using array or linked list. Let’s begin with the array
implementation.

As discussed earlier in Programming Fundamentals course, array is a data


type which is of fixed capacity and can store a collection of elements.
However we can use it to implement the list data structure.

For implementation we will be using list data type in python which is


internally a dynamic array which can grow and shrink based on the
elements added to or removed from it. Initially, it is created with a certain
capacity and based on elements getting added or removed, the capacity is
increased or decreased respectively.
To begin with, let’s create an empty list.

Try out the code given in the code pane for creating an empty list and
observe the result.

#Do not remove the below import statement

import sys

'''This function provides the capacity, size and space left in the list.

You can invoke it to get the details of the list'''

def list_details(lst):

#Number of elements that can be stored in the list

print("Capacity:", (sys.getsizeof(lst)-36)//4)

#Number of elements in the list

print("Size:", len(lst))

#Number of elements that can be accommodated in the space left

print("Space Left:", ((sys.getsizeof(lst)-36) - len(lst*4))//4)

#formula changes based on the system architecture

#(size-36)/4 for 32 bit machines and

#(size-64)/8 for 64 bit machines

# 36, 64 - size of an empty list based on machine

# 4, 8 - size of a single element in the list based on machine

marias_lst=[]

print("Empty list created!!!")

print("List details:")
list_details(marias_lst)

OUTPUT:

Empty list created!!!

List details:

Capacity: 0

Size: 0

Space Left: 0

Now Maria wants to add the first item, Sugar to the list.

When an element is added to an empty list in Python, a block of memory is


allocated and element is added at index position 0. The remaining memory is
considered to be reserved space which will be used later for addition or insertion
of elements.

Next
Algorithm steps:
add(element):
1. When the list is initially created, it is created
with a certain capacity.
2. While adding the elements, if the list is filled
to the capacity,
a. Create a new list with increased capacity
b. Copy the elements of initial list to the new list
3. Add the element to the end of the existing elements
in the list

append() method of Python list implicitly implements the


above algorithm.

Next
Algorithm steps:
insert(pos, element):
1. If the list is filled to capacity
a. Create a new list with increased capacity
b. Copy the elements of initial list to the new list
2. Shift right all the existing elements from index
position (pos) by 1 position
3. Insert the element at index position (pos)

You might also like