16 List
16 List
Online Course
Supported by
* All copyrights belong to UNESCO UNITWIN and it is prohibited to use these educational materials including the video and other
relevant materials for commercial and for-profit use.
List
16
Global Leadership School
Handong Global University
Learning Objectives
• Understand Lists
• Use operators on the Lists
• Use methods with Lists
• Understand 2D Lists
List
• List is sequence of values
• The components in the list
• Can store different data types
• They are called elements or items
• Can change value of list element/items
>>> cheeses = ['Cheddar', 'Edam', 'Gouda']
>>> numbers = [17, 123]
>>> print(numbers)
[17, 123]
>>> cheeses[0]
Cheddar
>>>numbers[1]
123
>>> numbers
[17, 123]
List – in operator
>>> cheeses = ['Cheddar', 'Edam', 'Gouda']
>>> cheeses
['Cheddar', 'Edam', 'Gouda']
for i in range(len(numbers)) :
numbers[i] = numbers[i] * 2
print(numbers[i])
print(numbers)
List, Operator
• Operator # The + operator concatenates lists
• +: Can join two lists to >>> a = [1, 2, 3]
create new list >>> b = [4, 5, 6]
• *: List * integer dupl >>> c = a + b
icate list content inte >>> c
ger times [1, 2, 3, 4, 5, 6]
for i in range(num):
print(“index”, i)
t = input(”Enter ta number to add: ")
tempList = [t]
NewList = NewList + tempList
print(NewList)
List, Methods
.append() Adds an element at the end of the list
#method pop(index)
>>> t
[‘b', ‘d‘, ‘e’]
>>> x
‘ac’
.remove()
# method remove(value)
f3.append('blackberry')
f3.sort()
print("after sorting = ", f3)
Method Example 2
f1=['apple', 'blueberry‘, 'melon', 'tomato']
f2=['strawberry', 'lemon', 'banana']
f3=f1+f2
print(f3)
i=0
while i < total:
if f3[i][0] == "b" :
f3.remove(f3[i])
i=i-1
total=total-1
i=i+1
nameList = []
for ch in name :
nameList.append(ch)
print(nameList)
count = 0
for ch in nameList :
if ch == chDel :
count = count + 1
for i in range(count) :
nameList.remove(chDel)
print(nameList)
Exercise 3
• Receive a string
• Make alphabets should be lowercase
• Arrange the alphabets in order
• All spaces should be deleted
Exercise 3, Code and Result
word = input(”Enter a word: ")
word = word.lower()
wordList = [] f
or ch in word :
wordList.append(ch)
print(wordList)
wordList.sort()
while wordList[0] == ' ':
wordList.remove(' ')
print(wordList)
2-dimensional List
• Create 2D list
• List within a list
• s = [ ["kim", 90, 75] , ["park", 80, 95] , ["choi", 76, 85] ]
S[0][2]
S[0][0]
S[0][1]
S[1][0]
S[2][1]
Using 2-dimensional List
fq= [[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]]
for i in range(5):
for j in range(6):
fq[i][ j] = i+j
print(i,"th row : ", fq[i])
print("="*50)
print("all : ", fq)
print("="*50)
2D list, Grade
s = [ ["kim", 90, 75], ["park", 89, 95], ["choi", 76, 85] ]
print( s )
for i in range( len(s) ):
print( s[i][0] )
sum=0
for j in range( 1, len(s[i]) ):
sum = sum + s[i][ j]
for i in range(15):
print(mul[i]) #print by row
print("Start!!")
for i in range(15):
for j in range(10):
mul[i][ j] = (i+2) * ( j+1)
print(mul[i]) #print by row
print("Done!!")
Exercise 5
• Make a unit matrix
• First, receive the size of the unit matrix.
• Create and output a unit matrix with a 2D list.
Exercise 5 Code
Size = int(input(”Size of matrix: "))
Unit_Matrix = []
for a in range(Size):
temp_row = []
for b in range(Size):
temp_row.append(0)
Unit_Matrix.append(temp_row)
for i in range(Size):
for j in range(Size):
if i == j:
Unit_Matrix[i][ j] = 1
else:
Unit_Matrix[i][ j] = 0
for i in range(Size):
print(Unit_Matrix[i])
Lecture Summary
• Understand Lists
• Sequence of values
• Can store different data types
• Can change value of list element/item
• Use operators on the List
• + : Join two lists to create new list
• * : (list) * (integer) duplicate list content integer times
• Use methods with Lists
• .append, .insert, .extend, .sort, .pop, .remove …
• Understand 2D Lists
• List within a list
Practice Problem 1
• What’s the result of the execution?
n = [1,3,5]
print(n * 2)
• [2, 6, 10]
• [2, 6, 10, 2, 6, 10]
• [1, 3, 5]
• [1, 3, 5, 1, 3, 5]
Practice Problem 1, Answer
• What’s the result of the execution?
n = [1,3,5]
print(n * 2)
• [2, 6, 10]
• [2, 6, 10, 2, 6, 10]
• [1, 3, 5]
• [1, 3, 5, 1, 3, 5]
Practice Problem 2
• What’s the result of the execution?
t1=[‘a‘, ‘b‘, ‘c‘]
t2=[‘A’, ‘B’]
t1.insert(1, ‘x’)
t1.extend(t2)
print(t1)
* All copyrights belong to UNESCO UNITWIN and it is prohibited to use these educational
materials including the video and other relevant materials for commercial and for-profit use.
CREDITS: This presentation template was created by Slidesgo, including icons by Flaticon, and infographics & images by Freepik.