Write a PYTHON function that receives list_a and returns a new list, list_b by e
ID: 3760743 • Letter: W
Question
Write a PYTHON function that receives list_a and returns a new list, list_b by eliminating the duplicate values in the list. Use the following function header: def eliminateDuplicates(list_a):
The main function reads a list of integers, invokes the function, and displays the result.
Hint: Read all the numbers and store them in list_a. Create a new list, list_b. Add a number in list_a to list_b. If the number is already in the list, ignore it.)
Sample output: Original list: [8 2 3 2 8 6 3 4 5 7 2 5 8] List without duplicates: [8 2 3 6 4 5 7]
Explanation / Answer
#!/usr/bin/python
import sys;
def eliminateDuplicates(list_a):
list_b = list();
i = 0;
while(i < len(list_a)-1):
num = list_a[i];
j = i+1;
duplicate = 0;
while(j < len(list_a)):
if(num == list_a[j]):
duplicate = 1;
break;
j += 1;
if(duplicate == 0):
list_b.append(list_a[i]);
i += 1;
list_b.append(list_a[i]);
return list_b;
def main():
list_a = list();
print 'Enter the list of elements. -1 to stop.';
i = 0;
n = 0;
while(n != -1):
n = input();
list_a.append(n);
i += 1;
list_b = eliminateDuplicates(list_a);
print list_b[0:len(list_b)-1];
if __name__ == "__main__":
sys.exit(main())
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.