Question

Write a program to remove all duplicate elements from a tuple and create a new tuple....

Write a program to remove all duplicate elements from a tuple and create a new tuple. Print the newly created tuple. If tuple is (10, 15, 8, 5,4,10,8,4,3,5) then the new tuple will be (10,15,8,5,4,3)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have included my code and screenshots in this answer. In case, there is any indentation issue due to editor, then please refer to code screenshots to avoid confusion.

--------------------main.py-------------------


tuple1 = (10, 15, 8, 5,4,10,8,4,3,5) #original tuple1

tuple2 = () #empty tuple2 to add elements without duplicates
for val in tuple1: #read each value of tuple1,
   if val not in tuple2: #check if that value is already existing in tuple2
       tuple2 = tuple2 + (val,) #if not exists then add element to tuple2

print("\nOriginal tuple: ", tuple1)   #print both tuples
print("\nNew tuple after removing duplicates: ", tuple2)  

--------------------Screenshot main.py-------------------

-/python/temp/main.py - Sublime Text (UNREGISTERED) 4:40 AM main.py war... RP tuplel = (10, 15, 8, 5,4,10,8,4,3,5) #original

--------------------Output------------------

vs@ubuntu:-/python/temp vs@ubuntu:-/python/temps python3 main.py Original tuple: (10, 15, 8, 5, 4, 10, 8, 4, 3, 5) New tuple

----------------------------------------------

I hope this helps you,

Please rate this answer if it helped you,

Thanks for the opportunity

Add a comment
Know the answer?
Add Answer to:
Write a program to remove all duplicate elements from a tuple and create a new tuple....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program to remove the duplicate elements of a given array and return the...

    Write a Java program to remove the duplicate elements of a given array and return the new length of the array. Sample array: [20, 20, 32, 76, 30, 40, 50, 50, 52] After removing the duplicate elements the program should return 6 as the new length of the array. Out put Original array length: 9 Array elements are: 20 20 32 76 30 40 50 50 52 The new length of the array is: 7

  • Write a java program to remove duplicate elements from an array. Do not use Java library...

    Write a java program to remove duplicate elements from an array. Do not use Java library utilities or 3rd party tools to solve this problem. You need to use the “for loop” and manage the index as you traverse thru the array [i ] while looking for duplicates. For example: Example 1 --------------------- Original Array: 105 123 -921 -1 123 8 8 8 -921 Array with unique values: 105 123 -921 -1 8 Example 2 --------------------------- Original Array: 10 22...

  • Write a Python program that uses a tuple. Create a tuple of your matric subjects. The...

    Write a Python program that uses a tuple. Create a tuple of your matric subjects. The program must display the first element, the last element, and the length of the tuple. An explanation message should also be included. E.g. The length of the tuple is 4. The user needs to be prompted for a subject, and the code needs to find the subject in the tuple. The user needs to be informed whether or not the subject is included in...

  • Write a C++ program to : Create array arr1 of size 10 Assign values to elements...

    Write a C++ program to : Create array arr1 of size 10 Assign values to elements such that - say index = i, arr1[i] = 10 - i. Print arr1 Create arr2 and copy alternate elements from arr1 starting from index 1. Print arr2. Sort arr1 in ascending array and print its elements from indices 3 to 8 Print every output on a different line and separate array elements by spaces.

  • python #Ignore line 2, it is just there to align with zybooks requirements print("5") #Create a...

    python #Ignore line 2, it is just there to align with zybooks requirements print("5") #Create a list with the following numbers and assign it to a variable name: 1,2,3,4,5,6 #Convert the list to a tuple and assign it to a variable name (dirrerent from the list's name) #Try the following and press the run program button to which ones work and which ones cause errors #If a statement causes an error, use a # to comment it out #Add the...

  • Create the program in C++ please. The new operator as outlined in 10.9 . Create a...

    Create the program in C++ please. The new operator as outlined in 10.9 . Create a Test class that includes the following Data members • First Name, last name, test1, test2, test3 o Methods • Accessor methods for each of the data members' • Mutator methods for each of the data members . A Default Constructor . A constructor that will accept arguments for each of the 5 data members • A method that will calculate the average of the...

  • Write a Java program that will create an array of Strings with 25 elements. Input Strings...

    Write a Java program that will create an array of Strings with 25 elements. Input Strings from the user and store them in the array until either the user enters “quit” or the array is full. HINT: the sentinel value is “quit” all lowercase. “Quit” or “QUIT” should not stop the loop. HINT: you have to use the equals method (not ==) when you are comparing two Strings. After the input is complete, print the Strings that the user entered...

  • Write a C++ program - create a 1-d array. Its data type is integer. It has...

    Write a C++ program - create a 1-d array. Its data type is integer. It has 10 elements. - initialize this array by 10 integer numbers. Five elements of the array are positive, and others are negative. - use a while loop to manipulate the array as below. - If the value of an element is positive, the value is doubled. Otherwise, the value is incremented by 3. - print the new value of each element. - use a for...

  • Write a C++ program with the following functionality. Place the even integers from 10 (inclusive) to...

    Write a C++ program with the following functionality. Place the even integers from 10 (inclusive) to 40 (inclusive) in a queue, Q. Remove the first 10 integers from Q and add to a stack, S. Pop and print all the elements from S.

  • write a java program Accept a positive integer n from keyboard and then create an array...

    write a java program Accept a positive integer n from keyboard and then create an array or arraylist containing n random elements within the range [-n,n). Print out the random array or arraylist, and then find out and print out the number of inversions and the maximum subarray (index range of the maximum subarray along with the maximum subarray sum) in the array or arraylist using divide and conquer, respectively. For example, suppose we accept integer 6 from keyboard, then...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT