Documente Academic
Documente Profesional
Documente Cultură
com)
C Programming Examples
Java Algorithms
C++ Algorithms
C Algorithms
Problem Description
The program takes two lists and finds the unions of the two lists.
Problem Solution
1. Define a function which accepts two lists and returns the union of them.
2. Declare two empty lists and initialise to an empty list.
3. Consider a for loop to accept values for 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 union of the two lists.
8. Print the union.
9. Exit.
Program/Source Code
Here is source code of the Python Program to find the union of two lists. The program output is also shown below.
a=[]
n= int(input("Enter the number of elements in list:"))
for x in range(0,n):
element=int(input("Enter element" + str(x+1) + ":"))
a.append(element)
b=[sum(a[0:x+1]) for x in range(0,len(a))]
print("The original list is: ",a)
print("The new list is: ",b)
Program Explanation
1. User must enter the number of elements in the list and store it in a variable.
2. User must enter the values to the same number of elements into the list.
3. The append function obtains each element from the user and adds the same to the end of the list as many times as the number of elements taken.
4. The same of 2 and 3 is done for the second list also.
5. The union function accepts two lists and returns the list which is the union of the two lists, i.e, all the values from list 1 and 2 without redundancy.
6. The set function in the union function accepts a list and returns the list after elimination of redundant values.
7. The lists are passed to the union function and the returned list is printed.
Case 1:
Enter the number of elements in list:5
Enter element1:10
Enter element2:10
Enter element3:20
Enter element4:20
Enter element5:20
Non-duplicate items:
[10, 20]
Case 2:
Enter the number of elements in list:4
Enter element1:23
Enter element2:56
Enter element3:67
Enter element4:10
('The original list is: ', [23, 56, 67, 10])
('The new list is: ', [23, 79, 146, 156])
Sanfoundry Global Education & Learning Series Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions (http://www.sanfoundry.com/python-problems-solutions/).
Prev Page - Python Program to Put Even and Odd elements in a List into Two Different Lists (http://www.sanfoundry.com/python-program-even-odd-elements-different-
lists/)
Next Page - Python Program to Remove All Tuples in a List of Tuples with the USN Outside the Given Range (http://www.sanfoundry.com/python-program-remove-tuples-
outside-range/)
A Vox
B Match.com
C eHarmony
D Zoosk Your Score 0 Question 1/10
Manish Bhojasia (http://www.sanfoundry.com/about/), a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at
Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in
Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux
Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage
Protocols such as iSCSI & Fiber Channel. Stay connected with him below:
LinkedIn (http://www.linkedin.com/in/manishbhojasia) | Facebook (http://www.facebook.com/sanfoundry) | Twitter
(http://www.twitter.com/sanfoundry) | Google+ (https://plus.google.com/104408026570656234343/posts)
Name*
Email*
Subscribe
Best Careers
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in
India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro,
Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.
Best Trainings
India Internships
About Sanfoundry
About Us (http://www.sanfoundry.com/about/)
Copyright (http://www.sanfoundry.com/copyright/)
TOS & Privacy (http://www.sanfoundry.com/tos-privacy-policy/)
Jobs (http://www.sanfoundry.com/jobs/)
Bangalore Training (http://www.sanfoundry.com/coursesfees/)
Online Training (http://www.sanfoundry.com/online-training-san-linux-kernel-device-drivers/)
SAN Training (http://www.sanfoundry.com/san-storage-area-networks-training/)
Developers Track (http://www.sanfoundry.com/salary-50l/)
Mentoring Sessions (http://www.sanfoundry.com/professional-mentoring-coaching-career-guidance-cto/)
Contact Us (http://www.sanfoundry.com/contact/)
Sitemap (http://www.sanfoundry.com/sitemap_index.xml)
2011-2017 Sanfoundry