0% found this document useful (0 votes)
31 views25 pages

L5 List Container

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)
31 views25 pages

L5 List Container

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/ 25

ED5340 - Data Science: Theory at h y

and Practise a p
gan
th u
M u
a n
th
L5 - List container m a n a
R a

Ramanathan Muthuganapathy (https://ed.iitm.ac.in/~raman)


Course web page: https://ed.iitm.ac.in/~raman/datascience.html
Moodle page: Available at https://courses.iitm.ac.in/
Container data type

• They can hold multiple data types.


h y
at
• Lists, Tuples, Sets, Dictionaries an
a p
u g
uth
• Also called as collections / compound
th a n data types
M
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


List representation

• List is a collection of dissimilar data types (mostly used for similar datatype)
h y
at
• lst1 = [10, 20, 300, 400, 50]; lst2 = [‘rat’,a‘cat’,
na p ‘bat’, ‘lion’, ‘tiger’, ‘crocodile’]
u g
uth
• lst3 = [10.5, 22, ‘Antelope’, ‘rabbit’,
th a n 456789, 1, 1, 2, 2, 4, 89.9]
M
n a
m a
• lst4 = [100] * 5 R a

• lst5 - [ ] #Empty list, basically empty ‘square’ brackets.

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


List basics and accessing

• Elements in the list can be repeated.


h y
at
• It is an ordered collection - can be indexedanand
ap sliced
u g
uth
• Entire list or each element can be tprinted.
h a n M
n a
a
m
• List is an `iterable’ i.e. you cana
R iterate over its elements.

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


y

Demo using
at h
a p
gan
th u
u

L5_list_ex_access.py
n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


h y
at
CW: Define a list of strings and find the a p
gan
th u
u

number of characters in each of them.


n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


List operations

• Lists are mutable (unlike strings).


h y
at
• Lists can be concatenated an
a p
u g
uth
• searching (containment) and sorting
th a n M
n a
am
• deletion - using index or a
range
R of indices

• conversion / other functions - str to list, len, max, min, sum


• shallow copy, deep copy, difference
• Lists comparison
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
y

Demo using
at h
a p
gan
th u
u

L5_list_operations.py
n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


CW: Define a list of marks for anastudent at h
p
y
and find the
max, min and average of them. M u th u g a
Arranged the in sorted
order. Print all of them clearly.
a n a th a n
Delete the entire list and
check for emptiness.R a m

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


CW: 1) Define two lists of strings. Use relational p a t h y operators to
a
compare them. What can you say? uga n
th
2) Instead of both lists as strings,n Mchange
u one of them to integers
a
and then use relational operators.
a n a t h What can you say?
a m
R

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


List methods - Member functions in the list

• Given a list, you can apply the following member functions using the object.
• append - at the end h y
pat
a
gan
• remove - the element th u
M u
a n
• pop - removes last item (also removes a particular
a t h item, if given)
a n
a m
• insert - after a certain given position
R

• reverse
• sort - both ascending and descending are available
• count
• index - of a particular item

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


List methods - Member functions in the list
Example

lst = [10, 20, 30, 30, 50, 60]


h y
lst.append(25) pat
a
gan
th u
u
lst.remove(30) a n M
ath
a n
a m
lst.pop( ) R

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


y

Demo using
at h
a p
gan
th u
u

L5_list_functions.py
n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


h y
CW: Start from empty list. Add few elements.
a na p Sortatthe list in the
reverse order. Delete the elementsuthone by one till the list is empty.
u g
n M
h a
nat
HW: Count the number ofRaoccurrences of each element in the list.
m a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


y

Demo using
at h
a p
gan
th u
u

L5_list_copy_comp.py
n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


List Varieties

• List of lists
h y
at
• List embedding an
a p
u g
uth
• List unpacking (using * operator) th a n M
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


y

Demo using
at h
a p
gan
th u
u

L5_list_varieties.py
n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


List Comprehension
shorter form for list creation

The syntax goes like this:


h y
lst = [expression for var in sequence [optional at
p for and/or if]]
a
an g
th u
u
The above is a replacement for the hfollowing
an
M
a t
an
m
lst = [ ] #empty list Ra

for var in sequence:

lst.append(expression)

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


y

Demo using
at h
a p
gan
th u
u

L5_list_comprehension.py
n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


h y

Demo using pat


a
gan
th u
u

L5_list_of_lists_comprehension.py
n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Some nuances on list and list of lists

• Lists are like 1D array (though not contiguous in mem. allocation)


h y
at
• List of lists are like 2D array (matrix) an
a p
u g
uth
n M
th a
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Some nuances on list and list of lists
Lists are like 1D array (row vector)

• lst = [10, 20, 30, 40]


h y
at
• for num in lst: an
a p
u g
uth
• num iterates over each element inthlst
an (which is an iterable) using ‘for’
M
n a
m a
R a

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Some nuances on list and list of lists
Lists are like 2D array (matrix)

arr = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]


h y
for ele in arr: pat
a
gan
th u
u
for num in ele: a n M
ath
a n
a m
print num R

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Some nuances on list and list of lists
Lists are like 2D array (matrix)

arr = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]


h y
for ele in arr: pat
a
gan
th u
print(ele) M u
a n
a th
ele iterates over each element in arr (which
a n is an iterable) using ‘for’
a m
R
What is each element of arr? - i.e. Each row - arr[0], arr[1] etc

arr[0] = [1, 2, 3, 4]

arr[1] = [5, 6, 7, 8]

So, what will the print statement give?

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras


Some nuances on list and list of lists
Lists are like 2D array (matrix)

Remember, arr[0] = [1, 2, 3, 4], arr[1] = [5, 6, 7, 8] and so on


h y
for num in ele: pat
a
gan
th u
u
print(num) a n M
a th
a n
a m
num will iterate over each eleR- i.e. iterate over arr[0], arr[1] etc.

iterate over arr[0] —> each element in arr[0]

So, What will be the print(num) give?

Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy