0% found this document useful (0 votes)
7 views4 pages

Career Enhancement Topic 3 List - Tuple - Set - Dictionary

Uploaded by

vashutyagi761
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)
7 views4 pages

Career Enhancement Topic 3 List - Tuple - Set - Dictionary

Uploaded by

vashutyagi761
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/ 4

1.

Problem Solving with Lists


Code-
# Original list
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# List comprehension to get even numbers


even_numbers = [num for num in numbers if num % 2 == 0]

print(even_numbers)

Output:
[2, 4, 6, 8, 10]

2. Problem Solving with Tuples

Code-

# List of tuples with (student_name, grade)

students = [("Alice", 85), ("Bob", 90), ("Charlie", 78), ("David", 92)]

# Find the student with the highest grade

best_student = max(students, key=lambda student: student[1])

print(f"Best student: {best_student[0]}, Grade: {best_student[1]}")

Output:

Best student: David, Grade: 92


3. Problem Solving with Sets
Code-
# Two lists
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]

# Convert lists to sets for set operations


set1 = set(list1)
set2 = set(list2)

# Intersection (common elements)


common_elements = set1 & set2

# Union (all unique elements)


unique_elements = set1 | set2

print(f"Common elements: {common_elements}")


print(f"Unique elements: {unique_elements}")

Output-
Common elements: {4, 5}
Unique elements: {1, 2, 3, 4, 5, 6, 7, 8}

4. Problem Solving with Dictionaries


Code-
# Input sentence
sentence = "apple banana apple orange banana apple"

# Split the sentence into words


words = sentence.split()

# Create an empty dictionary to count word occurrences


word_count = {}

for word in words:


if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1

print(word_count)

Output-
{'apple': 3, 'banana': 2, 'orange': 1}
5. Combining Multiple Data Structures
Code-
# Dictionary with student names and grades
students = {
"Alice": 85,
"Bob": 90,
"Charlie": 78,
"David": 92
}

# Threshold grade
threshold = 80

# Get students who scored above the threshold


top_students = [name for name, grade in students.items() if grade > threshold]

print(f"Students scoring above {threshold}: {top_students}")

Output-
Students scoring above 80: ['Alice', 'Bob', 'David']

6 Problem Solving with Nested Data Structures


Code-
# List of dictionaries with student information
students_info = [
{"name": "Alice", "age": 17, "grade": 85},
{"name": "Bob", "age": 19, "grade": 90},
{"name": "Charlie", "age": 18, "grade": 78},
{"name": "David", "age": 20, "grade": 92}
]

# Extract the names of students older than 18


adult_students = [student["name"] for student in students_info if student["age"] > 18]

print(f"Adult students: {adult_students}")

Output-
Adult students: ['Bob', 'David']

You might also like