a Collection Steps for Completion 1. Define a function named unite_lists. 2. Using a for statement, iterate through the two lists, while finding the common elements in both lists without duplicates. Snippet 6.109 shows an example output: >>> unite_lists([2,4,6,8], [3,6,9,12]) [2, 4, 6, 8, 3, 9, 12] 1 # Write your code here 2 unite_lists = [] 3 for items in a: 4 5 6 7 8 9 for numbers in b: if items == numbers: if items in c: None else: c.append(items) 10 # Test your code here 11 if __name__ == '__main__': 12 13 print (unite_lists([2,4,6,8], [3,6,9,12]))

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter17: Linked Lists
Section: Chapter Questions
Problem 18PE
icon
Related questions
Question

Although working with sets is quick and easy in Python, sometimes we might want to find the union between two lists. Create a function called unite_lists, which uses a loop to go through both lists and return common elements. Do not use the built-in set function

a Collection
Steps for Completion
1. Define a function named unite_lists.
2. Using a for statement, iterate through the
two lists, while finding the common elements
in both lists without duplicates.
Snippet 6.109 shows an example output:
>>> unite_lists([2,4,6,8], [3,6,9,12])
[2, 4, 6, 8, 3, 9, 12]
1 # Write your code here
2 unite_lists = []
3 for items in a:
4
5
for numbers in b:
if items == numbers:
if items in c:
None
6
7
8
9
10 # Test your code here
11 if __name__ == '__main__':
12
13
else:
c.append(items)
print(unite_lists([2,4,6,8], [3,6,9,12]))
Transcribed Image Text:a Collection Steps for Completion 1. Define a function named unite_lists. 2. Using a for statement, iterate through the two lists, while finding the common elements in both lists without duplicates. Snippet 6.109 shows an example output: >>> unite_lists([2,4,6,8], [3,6,9,12]) [2, 4, 6, 8, 3, 9, 12] 1 # Write your code here 2 unite_lists = [] 3 for items in a: 4 5 for numbers in b: if items == numbers: if items in c: None 6 7 8 9 10 # Test your code here 11 if __name__ == '__main__': 12 13 else: c.append(items) print(unite_lists([2,4,6,8], [3,6,9,12]))
Expert Solution
Step 1

The source code of the program

def unite_lists(lst1, lst2):
    result = []
    for n in lst1 + lst2:
        if n not in result:
            result.append(n)
    return result
if __name__ == '__main__':
    print(unite_lists([2, 4, 6, 8], [3, 6, 9, 12]))

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Structure
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning