A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/python/python_set_exercises.htm below:

Python - Set Exercises

Python - Set Exercises Python Set Exercise 1

Python program to find common elements in two lists with the help of set operations −

l1=[1,2,3,4,5]
l2=[4,5,6,7,8]
s1=set(l1)
s2=set(l2)
commons = s1&s2 # or s1.intersection(s2)
commonlist = list(commons)
print (commonlist)

It will produce the following output

[4, 5]
Python Set Exercise 2

Python program to check if a set is a subset of another −

s1={1,2,3,4,5}
s2={4,5}
if s2.issubset(s1):
   print ("s2 is a subset of s1")
else:
   print ("s2 is not a subset of s1")

It will produce the following output

s2 is a subset of s1
Python Set Exercise 3

Python program to obtain a list of unique elements in a list −

T1 = (1, 9, 1, 6, 3, 4, 5, 1, 1, 2, 5, 6, 7, 8, 9, 2)
s1 = set(T1)
print (s1)

It will produce the following output

{1, 2, 3, 4, 5, 6, 7, 8, 9}

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4