Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Take two lists, say for example these two:
- # a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
- # b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
- # and write a program that returns a list that contains only the elements that are common between the lists (without duplicates).
- # Make sure your program works on two lists of different sizes.
- #Python code to generate
- # random numbers and
- # append them to a list
- import random
- # Function to generate
- # and append them
- # start = starting range,
- # end = ending range
- # num = number of
- # elements needs to be appended
- def Rand(start, end, num):
- res = []
- for j in range(num):
- res.append(random.randint(start, end))
- return res
- # Driver Code
- num = int(input("Enter the range for first list : "))
- start = int(input("Enter the starting number : "))
- end = int(input("ENter the ending number : "))
- list1 = Rand(start, end, num)
- print(list1)
- num2 = int(input("Enter the range for second list : "))
- start2 = int(input("Enter the starting number : "))
- end2 = int(input("ENter the ending number : "))
- list2 = Rand(start2, end2, num2)
- print(list2)
- final = []
- for i in list1:
- if i in list2:
- final.append(i)
- print("The list containing common integers : ")
- set = set(final)
- list = list(set)
- print(list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement