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

program 3

The document describes a program that finds the intersection of two lists using Python sets. It explains the properties of sets, including their unordered and unchangeable nature, and provides a step-by-step guide to implement the program. The program prompts the user to input elements for two lists and then computes and displays their intersection.

Uploaded by

newshunt535
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)
6 views

program 3

The document describes a program that finds the intersection of two lists using Python sets. It explains the properties of sets, including their unordered and unchangeable nature, and provides a step-by-step guide to implement the program. The program prompts the user to input elements for two lists and then computes and displays their intersection.

Uploaded by

newshunt535
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/ 5

TITLE : Program to find the intersection of two lists.

Problem Description : The program takes two lists and finds the intersection of the two lists.

THEORY/ ANALYSIS :

Set

A set is a collection which is unordered, unchangeable, and unindexed.

Sets are written with curly brackets.

Example

Create a Set:

thisset = {"apple", "banana", "cherry"}

print(thisset)

Set Items

Set items are unordered, unchangeable, and do not allow duplicate values.

Duplicates Not Allowed

Sets cannot have two items with the same value.

Example

Duplicate values will be ignored:

thisset = {"apple", "banana", "cherry", "apple"}

print(thisset)

Get the number of items in a set:

thisset = {"apple", "banana", "cherry"}

print(len(thisset))

Set Items - Data Types

Set items can be of any data type:

Example

String, int and boolean data types:


set1 = {"apple", "banana", "cherry"}

set2 = {1, 5, 7, 9, 3}

set3 = {True, False, False}

Access Items

You cannot access items in a set by referring to an index or a key.

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

Loop through the set, and print the values:

thisset = {"apple", "banana", "cherry"}

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

add() Adds an element to the set

clear() Removes all the elements from the set

copy() Returns a copy of the set

difference() Returns a set containing the difference between two or more


sets

difference_update() Removes the items in this set that are also included in another,
specified set

discard() Remove the specified item


intersection() Returns a set, that is the intersection of two other sets

intersection_update() Removes the items in this set that are not present in other,
specified set(s)

isdisjoint() Returns whether two sets have a intersection or not

issubset() Returns whether another set contains this set or not

issuperset() Returns whether this set contains another set or not

pop() Removes an element from the set

remove() Removes the specified element

symmetric_difference() Returns a set with the symmetric differences of two sets

symmetric_difference_update() inserts the symmetric differences from this set and another

union() Return a set containing the union of sets

update() Update the set with the union of this set and others

Program to find intersection of two sets

1. Define a function that accepts two lists and returns the intersection of them.

2. Declare two empty lists and initialize them to an empty list.

3. Consider a for loop to accept values for the two lists.

4. Take the number of elements in the list and store it in a variable.

5. Accept the values into the list using another for loop and insert into the list.

6. Repeat 4 and 5 for the second list also.

7. Find the intersection of the two lists.

8. Print the intersection.

9. Exit.

Program :

alist=[]
blist=[]

n1=int(input("Enter number of elements for list1:"))

n2=int(input("Enter number of elements for list2:"))

print("For list1:")

for x in range(0,n1):

element=int(input("Enter element" + str(x+1) + ":"))

alist.append(element)

print("For list2:")

for x in range(0,n2):

element=int(input("Enter element" + str(x+1) + ":"))

blist.append(element)

print("The intersection is :")

result = list(set(a) & set(b))

print(“Intersection of two list is”)

print(result)

Run Time Test Cases

Case 1:

Enter number of elements for list1:3

Enter number of elements for list2:4

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]

You might also like