Problems on Datastructure
๐ 1. Access Element from a List
Problem:
You have a list of fruits. Print the second fruit.
Example Input:
fruits = ["apple", "banana", "cherry"]
Example Output:
banana
Explanation:
List indexing starts at 0. So fruits[1]
gives the second element.
๐ 2. Replace an Element in a List
Problem:
Replace โbananaโ with โorangeโ in the list.
Example Input:
fruits = ["apple", "banana", "cherry"]
Example Output:
['apple', 'orange', 'cherry']
Explanation:
Lists are mutable, so you can directly modify elements using index assignment.
โจ 3. Combine Two Lists
Problem:
Join two lists of numbers into one.
Example Input:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
Example Output:
[1, 2, 3, 4, 5, 6]
Explanation:
Using +
merges two lists into a single one.
๐ข 4. Convert String to List
Problem:
Convert a comma-separated string into a list.
Example Input:
text = "apple,banana,cherry"
Example Output:
['apple', 'banana', 'cherry']
Explanation:
The split()
method breaks a string into a list using a separator.
๐ค 5. Join List into String
Problem:
Convert a list into a single string separated by spaces.
Example Input:
words = ["Python", "is", "fun"]
Example Output:
Python is fun
Explanation:
join()
combines list elements into a single string using a chosen separator.
๐งพ 6. Access Dictionary Value
Problem:
Access the value of key "age"
from a dictionary.
Example Input:
person = {"name": "Amit", "age": 25}
Example Output:
25
Explanation:
Dictionaries store data in key-value pairs. You can retrieve values using the key name.
๐งฉ 7. Add New Key-Value Pair to Dictionary
Problem:
Add a new key "city": "Delhi"
to the given dictionary.
Example Input:
person = {"name": "Amit", "age": 25}
Example Output:
{'name': 'Amit', 'age': 25, 'city': 'Delhi'}
Explanation:
You can add new data to a dictionary by assigning a value to a new key.
๐ฌ 8. Combine List and Dictionary
Problem:
Given two lists โ one of keys and one of values โ create a dictionary.
Example Input:
keys = ["name", "age"]
values = ["Riya", 22]
Example Output:
{'name': 'Riya', 'age': 22}
Explanation:
zip()
pairs corresponding elements, and dict()
converts them into a dictionary.
๐งฎ 9. Extract Specific Data from Nested List of Dictionaries
Problem:
Given a list of dictionaries, print the name of the first student.
Example Input:
students = [
{"name": "Amit", "score": 85},
{"name": "Riya", "score": 90}
]
Example Output:
Amit
Explanation:
Access the first dictionary with [0]
and then fetch the "name"
value using its key.
๐ง 10. Nested Dictionary Access
Problem:
Print the subject name inside a nested dictionary.
Example Input:
student = {
"name": "Ravi",
"details": {"subject": "Maths", "marks": 90}
}
Example Output:
Maths
Explanation:
Access the inner dictionary using nested keys.
๐งพ 11. Extract Dictionary Keys as List
Problem:
Convert dictionary keys into a list.
Example Input:
student = {"name": "Riya", "age": 22, "marks": 85}
Example Output:
['name', 'age', 'marks']
Explanation:
The .keys()
method returns all dictionary keys, which can be converted to a list.
๐งฎ 12. Merge Two Dictionaries
Problem:
Merge two dictionaries into one.
Example Input:
a = {"x": 10, "y": 20}
b = {"z": 30}
Example Output:
{'x': 10, 'y': 20, 'z': 30}
Explanation:
The .update()
method adds key-value pairs from one dictionary to another.
๐ก 13. Replace Word in a String Using List
Problem:
Replace "Python"
with "Java"
in a sentence using list conversion.
Example Input:
sentence = "I love Python programming"
Example Output:
I love Java programming
Explanation:
Convert to a list, modify a word, then join it back into a string.
๐ง 14. Convert Tuple to List
Problem:
Convert a tuple into a list.
Example Input:
numbers = (10, 20, 30)
Example Output:
[10, 20, 30]
Explanation:
Use the list()
function to convert an immutable tuple into a mutable list.
๐ 15. Find Common Elements Between Two Lists Using Set
Problem:
Find the common fruits present in both lists.
Example Input:
list1 = ["apple", "banana", "cherry"]
list2 = ["banana", "cherry", "mango"]
Example Output:
{'banana', 'cherry'}
Explanation:
Converting lists to sets lets you find intersections easily using &
.