0% found this document useful (0 votes)
88 views23 pages

Pylists 2

The document discusses Python lists and their underlying implementation as arrays. It describes how lists can grow and shrink dynamically by expanding the underlying array when it is full. Common list operations like append, insert, remove, slice and their time complexities are examined.

Uploaded by

Ahmed Qazi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views23 pages

Pylists 2

The document discusses Python lists and their underlying implementation as arrays. It describes how lists can grow and shrink dynamically by expanding the underlying array when it is full. Common list operations like append, insert, remove, slice and their time complexities are examined.

Uploaded by

Ahmed Qazi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

The Python List

A mutable sequence type container.

Provides operations for managing the collection.

Can grow and/or shrink as needed.

Implemented using an array.

2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Chapter 2: Arrays

List: Construction

The Python list interface provides an abstraction to


the actual underlying implementation.
pyList = [ 4, 12, 2, 34, 17 ]

2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Chapter 2: Arrays

List: Implementation

An array is used to store the items of the list.

Created larger than needed.


The items are stored in a contiguous subset of the
array.

2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Chapter 2: Arrays

List: Appending an Item

New items can be added at the end of the list.


pyList.append(50)

When space is available, the item is stored in the


next slot.

2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Chapter 2: Arrays

List: Appending an Item

What happens when the array becomes full?


pyList.append(18)
pyList.append(64)
pyList.append(6)

There is no space for value 6.

2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Chapter 2: Arrays

Expanding The Array


Step 1: create a new array, double the size.

Step 2: copy the items from original array to the new array.

2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Chapter 2: Arrays

Expanding The Array


Step 3: replace the original array with the new array.

Step 4: store value 6 in the next slot of the new array.

2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Chapter 2: Arrays

Big-O Analysis of Append

Best Case Analysis?

Worst Case Analysis?

More later...

2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Chapter 2: Arrays

List: Extending

The entire contents of a list can be appended to a


second list.
pyListA = [34, 12]
pyListB = [4, 6, 31, 9]
pyListA.extend( pyListB )

2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Chapter 2: Arrays

Big-O Analysis of Extend

How should we measure input size?

Best Case Analysis?

Worst Case Analysis?

2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Chapter 2: Arrays

10

List: Inserting Items

An item can be inserted anywhere within the list.


pyList.insert( 3, 79 )

2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Chapter 2: Arrays

11

Big-O Analysis of Insert

Best Case Analysis?

Worst Case Analysis?

2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Chapter 2: Arrays

12

List: Removing Items

An item can be removed from position of the list.


pyList.pop(0)

2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Chapter 2: Arrays

13

List: Removing Items

Removing the last item in the list.


pyList.pop()

2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Chapter 2: Arrays

14

Big-O Analysis of Remove

Best Case Analysis?

Worst Case Analysis?

2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Chapter 2: Arrays

15

List: Slices

Slicing a list creates a new list from a contiguous


subset of elements.
aSlice = pyList[2:5]

2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Chapter 2: Arrays

16

Big-O Analysis of Slice

How do we measure input size?

Best Case Analysis?

Worst Case Analysis?

2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Chapter 2: Arrays

17

Python List: Time-Complexities


List Operation

Worst Case

v = list()

len(v)

v=[0]*n

v[i] = x

v.append(x)

v.extend(w)

v.insert(x)

v.pop()

traversal

2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Chapter 4: Algorithm Analysis 18

Python List: Time-Complexities


List Operation

Worst Case

v = list()

O(1)

len(v)

O(1)

v=[0]*n

O(n)

v[i] = x

O(1)

v.append(x)

O(n)

v.extend(w)

O(n) or O(n + m)

v.insert(x)

O(n)

v.pop()

O(n)

traversal

O(n)

2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Chapter 4: Algorithm Analysis 19

Revisiting Analysis of Append

Best Case: O(1)

Worst Case: O(n)

How would we analyze this:

for item in input:


myList.append(i)

2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Chapter 2: Arrays

20

Amortized Analysis

Compute the time-complexity by finding the


average cost over a sequence of operations.

Cost per operation must be known.

Cost must vary, with

many ops contributing little cost.


only a few ops contributing high cost.

2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Chapter 4: Algorithm Analysis 21

Append Example

2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Chapter 4: Algorithm Analysis 22

Amoritized Cost of Append

Total cost of resize operations:


lg n

j=0 2

< 2n O (n)
n

i=1 1=n

Total cost of set operations:

Average cost of n append operations:


lg n

j
2
j=0 +n

O (1)

2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Chapter 2: Arrays

23

You might also like