0% found this document useful (0 votes)
12 views

Lab Report 06

Python
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lab Report 06

Python
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

DEPARTMENT OF MECHANICAL ENGINEERING COLLEGE OF E&ME, NUST,

RAWALPINDI

Subject: Fundamental of programming

Lab Number # 06

SUBMITTED TO:

Instructor Name: Dr Asad Mansoor

Lab Engineer: Fatima Ismail

SUBMITTED BY:

Muhammad Talha

DE- 45 Syn A Mechanical Engineering

REG NO # 481402

SUBMITION DATE: 24 March 2024


Task 1: Question statement

Write a function that takes two lists of numbers and returns a new list that contains the elements
that are present in both lists. The length of the lists can be different.

CODE:

# Define two lists


list1 = [1, 2, 3, 4, 5]
list2 = [2, 4, 5, 6, 7]

# Initialize an empty list to store common elements


commen = []

# Iterate through each element in list1


for elem1 in list1:
# Iterate through each element in list2
for elem2 in list2:
# Check if the current element in list1 is equal to any element in list2
if elem1 == elem2:
# If they are equal, add the element to the common list
commen.append(elem1)

# Print the list of common elements


print(commen)

Output:

Task 2: Question statement

Write a function that takes two dictionaries and does the following:
• If a key exists in both the dictionaries, the values a added
• Else, the key: value pair is added to a new dictionary.
• The final dictionary is returned.
a = {‘one’: 12, ‘two’: 10, ‘three’:22},
b = {‘four’:14, ‘one’:10, ‘three’: 2}
c = {‘one’: 22, ‘two’: 10, ‘three’: 24, ‘four’: 14}

CODE:

def result(dict1, dict2):


# Initialize an empty dictionary to store the result
my_dict = {}

# Get the keys of the dictionaries


keys_dict1 = list(dict1.keys())
keys_dict2 = list(dict2.keys())

# Iterate through keys of dict1


for i in keys_dict1:
# Iterate through keys of dict2
for j in keys_dict2:
# If the key from dict1 matches the key from dict2
if (i == j):
# Add the values corresponding to the common key and store in
my_dict
my_dict[i] = dict1[i] + dict2[j]
# Break the loop as the common key is found
break
else:
# If no common key is found, add the value from dict1 to my_dict
my_dict[i] = dict1[i]

# Iterate through keys of dict2


for i in keys_dict2:
# Iterate through keys of dict1
for j in keys_dict1:
# If the key from dict2 doesn't match any key from dict1
if (i != j):
# Add the value from dict2 to my_dict
my_dict[i] = dict2[i]

# Return the result dictionary


return my_dict

# Define dictionaries
dict1 = {"one": 12, "two": 10, "three": 22}
dict2 = {"four": 14, "one": 10, "three": 2}
# Call the function and store the result
my_dict = result(dict1, dict2)

# Print the result dictionary


print(my_dict)

OUTPUTS:

**************************************************************************

You might also like