Lab Manual - AETN2302 - L7 (Lists-II)
Lab Manual - AETN2302 - L7 (Lists-II)
Lab Manual - AETN2302 - L7 (Lists-II)
1. If you arrive later than 10 minutes after the lab started, you will not be allowed to
do the lab.
3. There is no time allocated to redo any missed labs.
4. Absence from labs will result in a grade of zero unless a written medical excuse is
provided.
OBJECTIVES
1. Expanding upon experience with lists;
2. List Sorting, Editing, etc.
EQUIPMENT
1. Pcs
2. Sandbox
PART I:
Scenario: Imagine a list - not very long, not very complicated, just a simple list containing
some integer numbers. Some of these numbers may be repeated, and this is the clue. We don't
want any repetitions. We want them to be removed.
Your task is to write a program which removes all the number repetitions from the list. The goal
is to have a list in which all the numbers appear not more than once.
Test Data
List1= [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]
List2= [1, 2, 4, 6, 9]
Your code
list1 = [1,2,4,4,1,4,2,6,2,9]
list2 = []
for i in list1:
if i not in list2:
list2.append(i)
print("list1= ", list1)
print("list2= ", list2)
output:
list1= [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]
list2= [1, 2, 4, 6, 9]
list1 = [1,2,4,4,1,4,2,6,2,9]
list2 = []
for i in list1:
if i in list2:
continue
else:
list2.append(i)
print("list1= ", list1)
print("list2= ", list2)
output:
list1= [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]
list2= [1, 2, 4, 6, 9]
PART II
Scenario: In this part we want to get hand on experience on lists. To do so, let’s Create a list of
four names of your input. Then, we do the following:
1- Show the list.
2- Sort the list alphabetically
3- Show the list.
4- Reverse the list
5- Show the list.
6- Show the list length
Your code
list= []
for i in range(4):
list.append(input("enter name= "))
print(list)
list.sort()
print(list)
list.reverse()
print(list)
print(len(list))
enter name= a
enter name= d
enter name= s
enter name= d
['a', 'd', 's', 'd']
['a', 'd', 'd', 's']
['s', 'd', 'd', 'a']
4
Part III:
Scenario: In this part we want to get experience with 2-D lists. Consider the image below. We
want to create a matrix whose elements are filled out as the image. We need to create an 8 by 8
list and fill it out. Note that you have to find a formula for the elements based on i and j. For
example: 20 is equal to the length of 2 full rows plus 4. An incomplete code is given below. Use
it to develop your code.
matrix = []
for i in range(8):
row=[]
for j in range(8):
# enter element= function of (i,j)
row.append(element)
matrix.append(row)
print(matrix)
Your code
matrix=[]
for i in range(8):
row=[]
for j in range(8):
#enter element = function of (i,j)
element =(i)*8+j+1
row.append(element)
matrix.append(row)
print(row)
output:
[1, 2, 3, 4, 5, 6, 7, 8]
[9, 10, 11, 12, 13, 14, 15, 16]
[17, 18, 19, 20, 21, 22, 23, 24]
[25, 26, 27, 28, 29, 30, 31, 32]
[33, 34, 35, 36, 37, 38, 39, 40]
[41, 42, 43, 44, 45, 46, 47, 48]
[49, 50, 51, 52, 53, 54, 55, 56]
[57, 58, 59, 60, 61, 62, 63, 64]
Possible Earned
Activity Section Points Points