changing the original list inside function
in need to create one function that once remove duplicates while not changing the original list.
and in the second one it needs to change the original list and return nothing .
the problem here that the second function just works on one repeat and doesnt work on 2 different numbers( it just removes one number ( it doesnt work on [2,3,4,5,3,4] but works [ 1,2,3,3]
def drop_duplicates(lst):
s =
# Write the rest of the code for question 3a below here.
for i in lst:
if i not in s:
s.append(i)
return s
lst = [1, 2, 3, 2, 4, 2]
print drop_duplicates(lst)
print lst
def drop_duplicates_in_place(lst):
# Write the rest of the code for question 3b below here.
for i in range(len(lst) - 1):
for j in range ( i+1, len(lst) - 1):
if lst[i] == lst[j]:
lst.pop(j)
else:
continue
lst = [1, 2, 3, 2, 4, 2]
print lst
python python-2.7 duplicates
add a comment |
in need to create one function that once remove duplicates while not changing the original list.
and in the second one it needs to change the original list and return nothing .
the problem here that the second function just works on one repeat and doesnt work on 2 different numbers( it just removes one number ( it doesnt work on [2,3,4,5,3,4] but works [ 1,2,3,3]
def drop_duplicates(lst):
s =
# Write the rest of the code for question 3a below here.
for i in lst:
if i not in s:
s.append(i)
return s
lst = [1, 2, 3, 2, 4, 2]
print drop_duplicates(lst)
print lst
def drop_duplicates_in_place(lst):
# Write the rest of the code for question 3b below here.
for i in range(len(lst) - 1):
for j in range ( i+1, len(lst) - 1):
if lst[i] == lst[j]:
lst.pop(j)
else:
continue
lst = [1, 2, 3, 2, 4, 2]
print lst
python python-2.7 duplicates
add a comment |
in need to create one function that once remove duplicates while not changing the original list.
and in the second one it needs to change the original list and return nothing .
the problem here that the second function just works on one repeat and doesnt work on 2 different numbers( it just removes one number ( it doesnt work on [2,3,4,5,3,4] but works [ 1,2,3,3]
def drop_duplicates(lst):
s =
# Write the rest of the code for question 3a below here.
for i in lst:
if i not in s:
s.append(i)
return s
lst = [1, 2, 3, 2, 4, 2]
print drop_duplicates(lst)
print lst
def drop_duplicates_in_place(lst):
# Write the rest of the code for question 3b below here.
for i in range(len(lst) - 1):
for j in range ( i+1, len(lst) - 1):
if lst[i] == lst[j]:
lst.pop(j)
else:
continue
lst = [1, 2, 3, 2, 4, 2]
print lst
python python-2.7 duplicates
in need to create one function that once remove duplicates while not changing the original list.
and in the second one it needs to change the original list and return nothing .
the problem here that the second function just works on one repeat and doesnt work on 2 different numbers( it just removes one number ( it doesnt work on [2,3,4,5,3,4] but works [ 1,2,3,3]
def drop_duplicates(lst):
s =
# Write the rest of the code for question 3a below here.
for i in lst:
if i not in s:
s.append(i)
return s
lst = [1, 2, 3, 2, 4, 2]
print drop_duplicates(lst)
print lst
def drop_duplicates_in_place(lst):
# Write the rest of the code for question 3b below here.
for i in range(len(lst) - 1):
for j in range ( i+1, len(lst) - 1):
if lst[i] == lst[j]:
lst.pop(j)
else:
continue
lst = [1, 2, 3, 2, 4, 2]
print lst
python python-2.7 duplicates
python python-2.7 duplicates
edited Nov 22 '18 at 8:44
Mayank Porwal
4,9782725
4,9782725
asked Nov 22 '18 at 8:43
sereen masalhasereen masalha
91
91
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The inner loop range is too short:
for i in range(len(lst) - 1):
for j in range(i+1, len(lst)): # no -1 here
Furthermore, you do not really need the else
part in your loop body. Generally, nested loops and the repeated removing from (the middle) of a list are not ideal performance-wise. The recommended way too remove duplicates while maintaining order of occurrence in linear is by using an collections.OrderedDict
. To make it in-place, you can use slice assignment:
from collections import OrderedDict
def drop_duplicates_in_place(lst):
lst[:] = OrderedDict.fromkeys(lst)
Im trying to use away without import..
– sereen masalha
Nov 22 '18 at 8:59
@sereenmasalha For interests sake, this works in python 3.6+ without the import as dicts are ordered by default.lst[:] = dict.fromkeys(lst)
– SuperShoot
Nov 22 '18 at 9:53
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%2f53426911%2fchanging-the-original-list-inside-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The inner loop range is too short:
for i in range(len(lst) - 1):
for j in range(i+1, len(lst)): # no -1 here
Furthermore, you do not really need the else
part in your loop body. Generally, nested loops and the repeated removing from (the middle) of a list are not ideal performance-wise. The recommended way too remove duplicates while maintaining order of occurrence in linear is by using an collections.OrderedDict
. To make it in-place, you can use slice assignment:
from collections import OrderedDict
def drop_duplicates_in_place(lst):
lst[:] = OrderedDict.fromkeys(lst)
Im trying to use away without import..
– sereen masalha
Nov 22 '18 at 8:59
@sereenmasalha For interests sake, this works in python 3.6+ without the import as dicts are ordered by default.lst[:] = dict.fromkeys(lst)
– SuperShoot
Nov 22 '18 at 9:53
add a comment |
The inner loop range is too short:
for i in range(len(lst) - 1):
for j in range(i+1, len(lst)): # no -1 here
Furthermore, you do not really need the else
part in your loop body. Generally, nested loops and the repeated removing from (the middle) of a list are not ideal performance-wise. The recommended way too remove duplicates while maintaining order of occurrence in linear is by using an collections.OrderedDict
. To make it in-place, you can use slice assignment:
from collections import OrderedDict
def drop_duplicates_in_place(lst):
lst[:] = OrderedDict.fromkeys(lst)
Im trying to use away without import..
– sereen masalha
Nov 22 '18 at 8:59
@sereenmasalha For interests sake, this works in python 3.6+ without the import as dicts are ordered by default.lst[:] = dict.fromkeys(lst)
– SuperShoot
Nov 22 '18 at 9:53
add a comment |
The inner loop range is too short:
for i in range(len(lst) - 1):
for j in range(i+1, len(lst)): # no -1 here
Furthermore, you do not really need the else
part in your loop body. Generally, nested loops and the repeated removing from (the middle) of a list are not ideal performance-wise. The recommended way too remove duplicates while maintaining order of occurrence in linear is by using an collections.OrderedDict
. To make it in-place, you can use slice assignment:
from collections import OrderedDict
def drop_duplicates_in_place(lst):
lst[:] = OrderedDict.fromkeys(lst)
The inner loop range is too short:
for i in range(len(lst) - 1):
for j in range(i+1, len(lst)): # no -1 here
Furthermore, you do not really need the else
part in your loop body. Generally, nested loops and the repeated removing from (the middle) of a list are not ideal performance-wise. The recommended way too remove duplicates while maintaining order of occurrence in linear is by using an collections.OrderedDict
. To make it in-place, you can use slice assignment:
from collections import OrderedDict
def drop_duplicates_in_place(lst):
lst[:] = OrderedDict.fromkeys(lst)
edited Nov 22 '18 at 8:52
answered Nov 22 '18 at 8:46
schwobasegglschwobaseggl
37.4k32442
37.4k32442
Im trying to use away without import..
– sereen masalha
Nov 22 '18 at 8:59
@sereenmasalha For interests sake, this works in python 3.6+ without the import as dicts are ordered by default.lst[:] = dict.fromkeys(lst)
– SuperShoot
Nov 22 '18 at 9:53
add a comment |
Im trying to use away without import..
– sereen masalha
Nov 22 '18 at 8:59
@sereenmasalha For interests sake, this works in python 3.6+ without the import as dicts are ordered by default.lst[:] = dict.fromkeys(lst)
– SuperShoot
Nov 22 '18 at 9:53
Im trying to use away without import..
– sereen masalha
Nov 22 '18 at 8:59
Im trying to use away without import..
– sereen masalha
Nov 22 '18 at 8:59
@sereenmasalha For interests sake, this works in python 3.6+ without the import as dicts are ordered by default.
lst[:] = dict.fromkeys(lst)
– SuperShoot
Nov 22 '18 at 9:53
@sereenmasalha For interests sake, this works in python 3.6+ without the import as dicts are ordered by default.
lst[:] = dict.fromkeys(lst)
– SuperShoot
Nov 22 '18 at 9:53
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%2f53426911%2fchanging-the-original-list-inside-function%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