- 4

Help with Python (my attempt included)?

• Objective: - Write a python code to remove all the duplicate items from a list. • Example: - input : [2,2, 3,3,4,4,5,6,7] - output: [2,3,4,5,6,7] • My try: https://code.sololearn.com/ctqBEp8oI536

10th Jul 2020, 1:06 PM
Sahil Aujla
Sahil Aujla - avatar
24 Réponses
+ 7
To make it clear: set() does not preserve the given order of a list. lst = [1,2,7,3,1,5,9,1,0,2,4] # print(set(lst)) # result is {0, 1, 2, 3, 4, 5, 7, 9} this is not preserving order ! res = [] [res.append(i) for i in lst if i not in res] print(res) # result is: [1, 2, 7, 3, 5, 9, 0, 4] this is correct: the evaluation is running from left to right
10th Jul 2020, 3:11 PM
Lothar
Lothar - avatar
+ 8
Hello and welcome to sololearn We can't do work homeworks here so you have to show your attempt so that we may help you :) What you have to do just paste your code here https://www.sololearn.com/post/75089/?ref=app https://www.sololearn.com/discuss/333866/?ref=app
10th Jul 2020, 1:14 PM
Nilesh
Nilesh - avatar
+ 6
I've edited your question to include your attempt.
11th Jul 2020, 8:08 AM
Fox
Fox - avatar
+ 5
Though You should show us your attempt first.. However, Hint(with most simple syntax according to me): s=list(input()) #Use it with list.count It gives the number of times a particular item occurs in a list... # Use if-else statements with it and for loops...
10th Jul 2020, 1:13 PM
Arctic Fox
Arctic Fox - avatar
+ 5
Sahil Aujla Yes, you have to show us your attempt before asking us to do your project.You could have asked "How to remove duplicate items in a list" and you have given sample input and output.. Often, "write a program" these words are considered as homework or assignment. Learn the way for asking questions. Take care for this next time.
10th Jul 2020, 1:47 PM
Arctic Fox
Arctic Fox - avatar
+ 4
Sahil Aujla , your code is perfect! 👍
10th Jul 2020, 4:20 PM
Lothar
Lothar - avatar
+ 4
Sahil Aujla , see the answer to your question in the attached file: https://code.sololearn.com/ct4FURuoVboH/?ref=app
11th Jul 2020, 6:31 AM
Lothar
Lothar - avatar
+ 3
꧁༒Rishabh༒꧂ actually i am learning python because it's my passion it's not a subject in my school....so i have no homework or assignments for it.... but yes i agree with you that my way of asking question was not right....
10th Jul 2020, 1:53 PM
Sahil Aujla
Sahil Aujla - avatar
+ 3
Use dict.fromkeys() to preserve ordering: mylist = [9, 9, 10, 2, 3, 10, 3, 8, 4, 9, 4, 5, 5, 6, 7, 7] mylist = list(dict.fromkeys(mylist)) print(mylist)
10th Jul 2020, 6:37 PM
rodwynnejones
rodwynnejones - avatar
+ 3
Sahil Aujla He is a platinum moderator. Moderators can edit or delete your question. They are the members of the community who maintain sololearn guidelines. They have much powers so, that they can maintain the guidelines throughout sololearn https://www.sololearn.com/discuss/1110689/?ref=app https://www.sololearn.com/discuss/1668682/?ref=app https://www.sololearn.com/discuss/869582/?ref=app https://www.sololearn.com/discuss/420352/?ref=app https://www.sololearn.com/discuss/625730/?ref=app https://www.sololearn.com/discuss/2361498/?ref=app https://www.sololearn.com/discuss/306394/?ref=app
11th Jul 2020, 1:38 PM
Arctic Fox
Arctic Fox - avatar
+ 3
Sahil Aujla Fox is a platinum moderator and and plat mods have the power to edit any question if they feel it's against the community rules and guidelines. https://www.sololearn.com/Moderator-Guidelines https://www.sololearn.com/Moderator-Code-of-Ethics
11th Jul 2020, 1:38 PM
Nilesh
Nilesh - avatar
+ 2
rodwynnejones , really nice code and a good idea to do the task in this way! 👍
10th Jul 2020, 7:34 PM
Lothar
Lothar - avatar
+ 2
Fox thank you so much..... now it really looks cool😊.... but how could you edit the question asked by me?
11th Jul 2020, 1:35 PM
Sahil Aujla
Sahil Aujla - avatar
+ 1
Mansi 👑 suael I want the order of the numbers to be same [2,3,4,5,6,7] but ysing set() will not give it because order does not matter in set().... so the duplicate items would be removed but the order will not be same by using set(). let's find some other way
10th Jul 2020, 1:35 PM
Sahil Aujla
Sahil Aujla - avatar
+ 1
suael no order is not preserved I tried it...... order is not preserved
10th Jul 2020, 1:50 PM
Sahil Aujla
Sahil Aujla - avatar
+ 1
Lothar yes you are absolutely right.... but also check out my code for doing the same thing. However your code is more simple. https://code.sololearn.com/ctqBEp8oI536/?ref=app
10th Jul 2020, 3:18 PM
Sahil Aujla
Sahil Aujla - avatar
+ 1
rodwynnejones what is the function of .fromkeys()
10th Jul 2020, 11:46 PM
Sahil Aujla
Sahil Aujla - avatar
+ 1
꧁༒Rishabh༒꧂ Nilesh ok now I got it...
12th Jul 2020, 6:48 AM
Sahil Aujla
Sahil Aujla - avatar
0
Nilesh ꧁༒Rishabh༒꧂ I was not knowing that I am supposed to show my attempt also..... sorry for that
10th Jul 2020, 1:38 PM
Sahil Aujla
Sahil Aujla - avatar
0
Order is preserved , u gotta respect the intelligence of python. ..python knows what output you are expecting ;)
10th Jul 2020, 1:41 PM
Suhail KM