program 3
program 3
Problem Description : The program takes two lists and finds the intersection of the two lists.
THEORY/ ANALYSIS :
Set
Example
Create a Set:
print(thisset)
Set Items
Set items are unordered, unchangeable, and do not allow duplicate values.
Example
print(thisset)
print(len(thisset))
Example
set2 = {1, 5, 7, 9, 3}
Access Items
But you can loop through the set items using a for loop, or ask if a specified value is present in a
set, by using the in keyword.
Example
for x in thisset:
print(x)
Once a set is created, you cannot change its items, but you can add new items.
Set Methods
Python has a set of built-in methods that you can use on sets.
Method Description
difference_update() Removes the items in this set that are also included in another,
specified set
intersection_update() Removes the items in this set that are not present in other,
specified set(s)
symmetric_difference_update() inserts the symmetric differences from this set and another
update() Update the set with the union of this set and others
1. Define a function that accepts two lists and returns the intersection of them.
5. Accept the values into the list using another for loop and insert into the list.
9. Exit.
Program :
alist=[]
blist=[]
print("For list1:")
for x in range(0,n1):
alist.append(element)
print("For list2:")
for x in range(0,n2):
blist.append(element)
print(result)
Case 1:
For list1:
Enter element1:34
Enter element2:23
Enter element3:65
For list2:
Enter element1:33
Enter element2:65
Enter element3:23
Enter element4:86
The intersection is :
[65, 23]