Unit 6 Assignment – Python Lists Operations
Introduction
This assignment demonstrates how Python lists can be manipulated using operations such
as adding elements, deleting elements, merging lists, updating values, and sorting. Lists are
mutable sequences in Python, meaning their elements can be changed after creation
(Downey, 2015). The examples below simulate a data analysis scenario in which employee
data is processed using list operations.
Part (a): Employee List Operations
Python Code
# Step 1: Create a list of 10 employee names
employeeList = ["John Smith","Mary Johnson","David Lee","Sarah Adams","Michael Brown",
"Emma Wilson","Daniel Clark","Olivia Hall","James Scott","Sophia Turner"]
# Step 2: Split the list into two sublists of 5 employees each
subList1 = employeeList[:5]
subList2 = employeeList[5:]
print("SubList1:", subList1)
print("SubList2:", subList2)
# Step 3: Add a new employee to subList2
[Link]("Kriti Brown")
print("Updated SubList2:", subList2)
# Step 4: Remove the second employee from subList1
[Link](1)
print("Updated SubList1:", subList1)
# Step 5: Merge both lists
mergedList = subList1 + subList2
print("Merged Employee List:", mergedList)
# Step 6: Create a salary list for employees
salaryList = [4200, 3900, 4500, 4100, 4000, 4700, 3800, 4400, 4600, 4300]
# Step 7: Increase each salary by 4%
salaryList = [salary * 1.04 for salary in salaryList]
print("Updated Salary List:", salaryList)
# Step 8: Sort salary list in descending order
[Link](reverse=True)
# Step 9: Display top 3 salaries
print("Top 3 Salaries:", salaryList[:3])
Output (Example)
SubList1: ['John Smith','Mary Johnson','David Lee','Sarah Adams','Michael Brown']
SubList2: ['Emma Wilson','Daniel Clark','Olivia Hall','James Scott','Sophia Turner']
Updated SubList2:
['Emma Wilson','Daniel Clark','Olivia Hall','James Scott','Sophia Turner','Kriti Brown']
Updated SubList1:
['John Smith','David Lee','Sarah Adams','Michael Brown']
Merged Employee List:
['John Smith','David Lee','Sarah Adams','Michael Brown','Emma Wilson',
'Daniel Clark','Olivia Hall','James Scott','Sophia Turner','Kriti Brown']
Updated Salary List:
[4368.0,4056.0,4680.0,4264.0,4160.0,4888.0,3952.0,4576.0,4784.0,4472.0]
Top 3 Salaries:
[4888.0,4784.0,4680.0]
Technical Explanation
The employee list is first divided using Python slicing. The expression employeeList[:5]
creates subList1 containing the first five employees, while employeeList[5:] creates
subList2 with the remaining employees. The append() method adds the new employee 'Kriti
Brown' to subList2. The pop(1) method removes the second element from subList1 because
Python lists use zero-based indexing. The two lists are merged using the + operator, which
concatenates lists. A salary list is then created to store employee salaries. A list
comprehension multiplies each salary by 1.04 to apply a 4% salary increase. The
sort(reverse=True) method orders the salary values from highest to lowest, and slicing
(salaryList[:3]) selects the top three salaries.
Part (b): Sentence to Word List and Reverse
Python Code
# Step 1: Define a sentence
sentence = "Python programming helps analysts manage data efficiently"
# Step 2: Convert sentence into a list of words
wordList = [Link]()
print("Word List:", wordList)
# Step 3: Reverse the word list
[Link]()
print("Reversed Word List:", wordList)
Output (Example)
Word List:
['Python','programming','helps','analysts','manage','data','efficiently']
Reversed Word List:
['efficiently','data','manage','analysts','helps','programming','Python']
Technical Explanation
In this program, the split() method converts the sentence string into a list of words by
separating the string wherever spaces occur. Each word becomes an individual element in
the list. The reverse() list method then changes the order of the elements in the list so that
the last word becomes the first and the first word becomes the last. Because lists are
mutable in Python, the reverse() operation modifies the same list object rather than
creating a new list.
Reference
Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tea Press.
[Link]