- 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
24 odpowiedzi
+ 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
+ 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
+ 6
I've edited your question to include your attempt.
+ 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...
+ 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.
+ 4
Sahil Aujla , your code is perfect! 👍
+ 4
Sahil Aujla , see the answer to your question in the attached file:
https://code.sololearn.com/ct4FURuoVboH/?ref=app
+ 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....
+ 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)
+ 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
+ 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
+ 2
rodwynnejones , really nice code and a good idea to do the task in this way! 👍
+ 2
Fox thank you so much..... now it really looks cool😊.... but how could you edit the question asked by me?
+ 1
suael no order is not preserved I tried it...... order is not preserved
+ 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
+ 1
rodwynnejones what is the function of .fromkeys()
+ 1
꧁༒Rishabh༒꧂ Nilesh ok now I got it...
0
Nilesh ꧁༒Rishabh༒꧂ I was not knowing that I am supposed to show my attempt also..... sorry for that
0
Order is preserved , u gotta respect the intelligence of python. ..python knows what output you are expecting ;)