Python_two_parallel_lists_examples

Double List (students & marks)

You are given two parallel lists:

  • students = ["Alice", "Bob", "Charlie", "David", "Eve"]

  • marks = [85, 92, 78, 90, 88]

Step-by-Step Features for Beginners:

  1. Traverse → forward & backward

  2. Search → find a student’s marks

  3. Update → change marks

  4. Add → new student with marks

  5. Delete → student and marks

  6. Sort → by name & by marks

  7. Reverse → implicitly by descending sort

  8. Max/Min → highest and lowest marks

  9. Average → average marks

  10. Rank List → display sorted by marks




# Single List of Students
students = ["Alice", "Bobby", "Charlie", "David", "Frank", "Grace"]

# -----------------------------
# 1. Traverse the list (forward)
# -----------------------------
print("1. Traverse forward:")
for i in range(len(students)):
    print(students[i])

# -----------------------------
# 2. Traverse the list (backward)
# -----------------------------
print("\n2. Traverse backward:")
for i in range(len(students)-1, -1, -1):
    print(students[i])

# -----------------------------
# 3. Search for a student
# -----------------------------
search_student = "Charlie"
found = False
for i in range(len(students)):
    if students[i] == search_student:
        print("\n3. Search:", search_student, "found at index", i)
        found = True
        break
if not found:
    print("\n3. Search:", search_student, "not found")

# -----------------------------
# 4. Update a student's name
# -----------------------------
# Update Bobby to Bob
for i in range(len(students)):
    if students[i] == "Bobby":
        students[i] = "Bob"
        break
print("\n4. After Update:", students)

# -----------------------------
# 5. Add a new student at the end
# -----------------------------
students = students + ["Hannah"]
print("\n5. After Addition:", students)

# -----------------------------
# 6. Delete a student
# -----------------------------
# Delete Frank
for i in range(len(students)):
    if students[i] == "Frank":
        for j in range(i, len(students)-1):
            students[j] = students[j+1]
        students = students[:len(students)-1]
        break
print("\n6. After Deletion:", students)

# -----------------------------
# 7. Sort alphabetically (A-Z)
# -----------------------------
for i in range(len(students)):
    for j in range(i+1, len(students)):
        if students[i] > students[j]:
            temp = students[i]
            students[i] = students[j]
            students[j] = temp
print("\n7. Sorted Alphabetically (A-Z):", students)

# -----------------------------
# 8. Sort reverse alphabetically (Z-A)
# -----------------------------
for i in range(len(students)):
    for j in range(i+1, len(students)):
        if students[i] < students[j]:
            temp = students[i]
            students[i] = students[j]
            students[j] = temp
print("\n8. Reverse Sorted (Z-A):", students)

# -----------------------------
# 9. Reverse the list (without sort)
# -----------------------------
reversed_students = []
for i in range(len(students)-1, -1, -1):
    reversed_students = reversed_students + [students[i]]
print("\n9. Reversed List:", reversed_students)

# -----------------------------
# 10. First and last alphabetically
# -----------------------------
first_student = students[0]
last_student = students[0]
for i in range(1, len(students)):
    if students[i] < first_student:
        first_student = students[i]
    if students[i] > last_student:
        last_student = students[i]
print("\n10. First Alphabetically:", first_student)
print("10. Last Alphabetically:", last_student)

# -----------------------------
# 11. Count occurrences
# -----------------------------
count_student = "Alice"
count = 0
for i in range(len(students)):
    if students[i] == count_student:
        count += 1
print("\n11. Occurrences of", count_student, ":", count)

# -----------------------------
# 12. Insert at specific position
# -----------------------------
# Insert "Irene" at index 3
students = students[:3] + ["Irene"] + students[3:]
print("\n12. After inserting Irene at index 3:", students)

# -----------------------------
# 13. Second student alphabetically
# -----------------------------
sorted_students = students[:]
for i in range(len(sorted_students)):
    for j in range(i+1, len(sorted_students)):
        if sorted_students[i] > sorted_students[j]:
            temp = sorted_students[i]
            sorted_students[i] = sorted_students[j]
            sorted_students[j] = temp
second_student = sorted_students[1]
print("\n13. Second student alphabetically:", second_student)

# -----------------------------
# 14. Remove first student starting with 'D'
# -----------------------------
for i in range(len(students)):
    if students[i][0] == 'D':
        for j in range(i, len(students)-1):
            students[j] = students[j+1]
        students = students[:len(students)-1]
        break
print("\n14. After removing first student starting with 'D':", students)

# -----------------------------
# 15. Display students with name length > 4
# -----------------------------
print("\n15. Students with name length > 4:")
for i in range(len(students)):
    if len(students[i]) > 4:
        print(students[i])

Comments

Popular posts from this blog

Python_While_Loop

Python_Lists_Loops

clinical_app