Delete duplicates of a list whithout using using a secondary list
I have an exercise for my homeworks where I need to write a program that takes a list of integers as input, and returns the same list without any duplicate numbers.
The first idea that came to my mind is a loop using another list to compare with the first one, but we are not allowed to use another list.
python
add a comment |
I have an exercise for my homeworks where I need to write a program that takes a list of integers as input, and returns the same list without any duplicate numbers.
The first idea that came to my mind is a loop using another list to compare with the first one, but we are not allowed to use another list.
python
1
Well, if you are not allowed to use auxiliary storage, you are going to have to rely on mutator methods like.remove
ordel my_list[i]
Try something then ask a question if you run into problems, providing your Minimal, Complete, and Verifiable example. Otherwise, this is too broad.
– juanpa.arrivillaga
Nov 22 '18 at 17:11
1
Hey! Welcome to StackOverflow. Right now, your question is kinda generic, since we have no idea of what you've tried so far. I highly recomend you post your attempts so far. Also, this can be useful for asking about homework: meta.stackoverflow.com/questions/334822/…
– Helena Martins
Nov 22 '18 at 17:13
Note, your first instinct was on the right track, but generally you would use aset
as auxilliary storage in this case, because then you can remove duplicate in linear time!
– juanpa.arrivillaga
Nov 22 '18 at 17:14
Thank's for your answers, It finally worked perfectly with list(set()) . Pretty sure that's not what my teachers wanted me to do, since they always want us to use math with ugly loops instead of built-in fonctions (which is kinda stupid in my opinion) but anyway thank you!
– Axel
Nov 22 '18 at 18:16
add a comment |
I have an exercise for my homeworks where I need to write a program that takes a list of integers as input, and returns the same list without any duplicate numbers.
The first idea that came to my mind is a loop using another list to compare with the first one, but we are not allowed to use another list.
python
I have an exercise for my homeworks where I need to write a program that takes a list of integers as input, and returns the same list without any duplicate numbers.
The first idea that came to my mind is a loop using another list to compare with the first one, but we are not allowed to use another list.
python
python
edited Nov 22 '18 at 19:29
Aravind Bhat K
274214
274214
asked Nov 22 '18 at 17:08
Axel Axel
1
1
1
Well, if you are not allowed to use auxiliary storage, you are going to have to rely on mutator methods like.remove
ordel my_list[i]
Try something then ask a question if you run into problems, providing your Minimal, Complete, and Verifiable example. Otherwise, this is too broad.
– juanpa.arrivillaga
Nov 22 '18 at 17:11
1
Hey! Welcome to StackOverflow. Right now, your question is kinda generic, since we have no idea of what you've tried so far. I highly recomend you post your attempts so far. Also, this can be useful for asking about homework: meta.stackoverflow.com/questions/334822/…
– Helena Martins
Nov 22 '18 at 17:13
Note, your first instinct was on the right track, but generally you would use aset
as auxilliary storage in this case, because then you can remove duplicate in linear time!
– juanpa.arrivillaga
Nov 22 '18 at 17:14
Thank's for your answers, It finally worked perfectly with list(set()) . Pretty sure that's not what my teachers wanted me to do, since they always want us to use math with ugly loops instead of built-in fonctions (which is kinda stupid in my opinion) but anyway thank you!
– Axel
Nov 22 '18 at 18:16
add a comment |
1
Well, if you are not allowed to use auxiliary storage, you are going to have to rely on mutator methods like.remove
ordel my_list[i]
Try something then ask a question if you run into problems, providing your Minimal, Complete, and Verifiable example. Otherwise, this is too broad.
– juanpa.arrivillaga
Nov 22 '18 at 17:11
1
Hey! Welcome to StackOverflow. Right now, your question is kinda generic, since we have no idea of what you've tried so far. I highly recomend you post your attempts so far. Also, this can be useful for asking about homework: meta.stackoverflow.com/questions/334822/…
– Helena Martins
Nov 22 '18 at 17:13
Note, your first instinct was on the right track, but generally you would use aset
as auxilliary storage in this case, because then you can remove duplicate in linear time!
– juanpa.arrivillaga
Nov 22 '18 at 17:14
Thank's for your answers, It finally worked perfectly with list(set()) . Pretty sure that's not what my teachers wanted me to do, since they always want us to use math with ugly loops instead of built-in fonctions (which is kinda stupid in my opinion) but anyway thank you!
– Axel
Nov 22 '18 at 18:16
1
1
Well, if you are not allowed to use auxiliary storage, you are going to have to rely on mutator methods like
.remove
or del my_list[i]
Try something then ask a question if you run into problems, providing your Minimal, Complete, and Verifiable example. Otherwise, this is too broad.– juanpa.arrivillaga
Nov 22 '18 at 17:11
Well, if you are not allowed to use auxiliary storage, you are going to have to rely on mutator methods like
.remove
or del my_list[i]
Try something then ask a question if you run into problems, providing your Minimal, Complete, and Verifiable example. Otherwise, this is too broad.– juanpa.arrivillaga
Nov 22 '18 at 17:11
1
1
Hey! Welcome to StackOverflow. Right now, your question is kinda generic, since we have no idea of what you've tried so far. I highly recomend you post your attempts so far. Also, this can be useful for asking about homework: meta.stackoverflow.com/questions/334822/…
– Helena Martins
Nov 22 '18 at 17:13
Hey! Welcome to StackOverflow. Right now, your question is kinda generic, since we have no idea of what you've tried so far. I highly recomend you post your attempts so far. Also, this can be useful for asking about homework: meta.stackoverflow.com/questions/334822/…
– Helena Martins
Nov 22 '18 at 17:13
Note, your first instinct was on the right track, but generally you would use a
set
as auxilliary storage in this case, because then you can remove duplicate in linear time!– juanpa.arrivillaga
Nov 22 '18 at 17:14
Note, your first instinct was on the right track, but generally you would use a
set
as auxilliary storage in this case, because then you can remove duplicate in linear time!– juanpa.arrivillaga
Nov 22 '18 at 17:14
Thank's for your answers, It finally worked perfectly with list(set()) . Pretty sure that's not what my teachers wanted me to do, since they always want us to use math with ugly loops instead of built-in fonctions (which is kinda stupid in my opinion) but anyway thank you!
– Axel
Nov 22 '18 at 18:16
Thank's for your answers, It finally worked perfectly with list(set()) . Pretty sure that's not what my teachers wanted me to do, since they always want us to use math with ugly loops instead of built-in fonctions (which is kinda stupid in my opinion) but anyway thank you!
– Axel
Nov 22 '18 at 18:16
add a comment |
2 Answers
2
active
oldest
votes
You can use set
s=['a', 'b', 'c', 'd', 'a']
s=list(set(s))
print(s)
>>> ['b', 'c', 'd', 'a']
Well, as for the comments, you can use OrderedDict
from collections import OrderedDict
s=['a', 'b', 'c', 'd', 'a']
print(list(OrderedDict.fromkeys(s)))
1
I imagine if you are not allowed to use another list you are not allowed to use aset
, but even if you were, this would then become a clear duplicate.
– juanpa.arrivillaga
Nov 22 '18 at 17:13
2
This solution uses, even if implicitly, another list. :-)
– Sidon
Nov 22 '18 at 17:16
add a comment |
You can cast the list to a set and the cast that back to a list.
l = [1, 1, 2, 2, 3]
s = set(l)
list_without_duplicates = list(s)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435620%2fdelete-duplicates-of-a-list-whithout-using-using-a-secondary-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use set
s=['a', 'b', 'c', 'd', 'a']
s=list(set(s))
print(s)
>>> ['b', 'c', 'd', 'a']
Well, as for the comments, you can use OrderedDict
from collections import OrderedDict
s=['a', 'b', 'c', 'd', 'a']
print(list(OrderedDict.fromkeys(s)))
1
I imagine if you are not allowed to use another list you are not allowed to use aset
, but even if you were, this would then become a clear duplicate.
– juanpa.arrivillaga
Nov 22 '18 at 17:13
2
This solution uses, even if implicitly, another list. :-)
– Sidon
Nov 22 '18 at 17:16
add a comment |
You can use set
s=['a', 'b', 'c', 'd', 'a']
s=list(set(s))
print(s)
>>> ['b', 'c', 'd', 'a']
Well, as for the comments, you can use OrderedDict
from collections import OrderedDict
s=['a', 'b', 'c', 'd', 'a']
print(list(OrderedDict.fromkeys(s)))
1
I imagine if you are not allowed to use another list you are not allowed to use aset
, but even if you were, this would then become a clear duplicate.
– juanpa.arrivillaga
Nov 22 '18 at 17:13
2
This solution uses, even if implicitly, another list. :-)
– Sidon
Nov 22 '18 at 17:16
add a comment |
You can use set
s=['a', 'b', 'c', 'd', 'a']
s=list(set(s))
print(s)
>>> ['b', 'c', 'd', 'a']
Well, as for the comments, you can use OrderedDict
from collections import OrderedDict
s=['a', 'b', 'c', 'd', 'a']
print(list(OrderedDict.fromkeys(s)))
You can use set
s=['a', 'b', 'c', 'd', 'a']
s=list(set(s))
print(s)
>>> ['b', 'c', 'd', 'a']
Well, as for the comments, you can use OrderedDict
from collections import OrderedDict
s=['a', 'b', 'c', 'd', 'a']
print(list(OrderedDict.fromkeys(s)))
edited Nov 22 '18 at 17:16
answered Nov 22 '18 at 17:12
Sandesh34Sandesh34
254112
254112
1
I imagine if you are not allowed to use another list you are not allowed to use aset
, but even if you were, this would then become a clear duplicate.
– juanpa.arrivillaga
Nov 22 '18 at 17:13
2
This solution uses, even if implicitly, another list. :-)
– Sidon
Nov 22 '18 at 17:16
add a comment |
1
I imagine if you are not allowed to use another list you are not allowed to use aset
, but even if you were, this would then become a clear duplicate.
– juanpa.arrivillaga
Nov 22 '18 at 17:13
2
This solution uses, even if implicitly, another list. :-)
– Sidon
Nov 22 '18 at 17:16
1
1
I imagine if you are not allowed to use another list you are not allowed to use a
set
, but even if you were, this would then become a clear duplicate.– juanpa.arrivillaga
Nov 22 '18 at 17:13
I imagine if you are not allowed to use another list you are not allowed to use a
set
, but even if you were, this would then become a clear duplicate.– juanpa.arrivillaga
Nov 22 '18 at 17:13
2
2
This solution uses, even if implicitly, another list. :-)
– Sidon
Nov 22 '18 at 17:16
This solution uses, even if implicitly, another list. :-)
– Sidon
Nov 22 '18 at 17:16
add a comment |
You can cast the list to a set and the cast that back to a list.
l = [1, 1, 2, 2, 3]
s = set(l)
list_without_duplicates = list(s)
add a comment |
You can cast the list to a set and the cast that back to a list.
l = [1, 1, 2, 2, 3]
s = set(l)
list_without_duplicates = list(s)
add a comment |
You can cast the list to a set and the cast that back to a list.
l = [1, 1, 2, 2, 3]
s = set(l)
list_without_duplicates = list(s)
You can cast the list to a set and the cast that back to a list.
l = [1, 1, 2, 2, 3]
s = set(l)
list_without_duplicates = list(s)
answered Nov 22 '18 at 17:24
Pablo PaglillaPablo Paglilla
29715
29715
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435620%2fdelete-duplicates-of-a-list-whithout-using-using-a-secondary-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Well, if you are not allowed to use auxiliary storage, you are going to have to rely on mutator methods like
.remove
ordel my_list[i]
Try something then ask a question if you run into problems, providing your Minimal, Complete, and Verifiable example. Otherwise, this is too broad.– juanpa.arrivillaga
Nov 22 '18 at 17:11
1
Hey! Welcome to StackOverflow. Right now, your question is kinda generic, since we have no idea of what you've tried so far. I highly recomend you post your attempts so far. Also, this can be useful for asking about homework: meta.stackoverflow.com/questions/334822/…
– Helena Martins
Nov 22 '18 at 17:13
Note, your first instinct was on the right track, but generally you would use a
set
as auxilliary storage in this case, because then you can remove duplicate in linear time!– juanpa.arrivillaga
Nov 22 '18 at 17:14
Thank's for your answers, It finally worked perfectly with list(set()) . Pretty sure that's not what my teachers wanted me to do, since they always want us to use math with ugly loops instead of built-in fonctions (which is kinda stupid in my opinion) but anyway thank you!
– Axel
Nov 22 '18 at 18:16