im passing a list of integers to a function and removing duplicates of any integer. i want to modify the parameter that is passed, i read that lists are mutable so i converted the list to a set and then redefined the parameter list with the set because sets contain unique elements
error message: incorrect parameter values after call
#🔒 modifying parameter?
84 messages · Page 1 of 1 (latest)
@echo terrace
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
Could you paste your code snippet here?
def remove_duplicates(v):
s = set()
s.update(v)
v = list(s)
print(v)
v = [1,1,1,2,2,3,3,]
remove_duplicates(v)
print(v)
Hey @echo terrace!
It looks like you're trying to paste code into this channel.
Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
Also, paste the call
edited
You’re getting an error?
I can’t seem to recreate it
Also, if you want the passed list to be updated you need to return the updated list and assign it back to v.
you didn't modify the incoming list v at all here, you reassigned v to a new list and forgot the old list
to replace a list's contents with new contents, you can do this:
v[:] = new_items
where new_items can be a set, a list, a string, whatever, as long as it has items inside,
alternatively you can also do:
v.clear() # delete all list items
v.extend(new_items)
or the OP could just return the new list instead of modifying in place.
.
requirements to modify in the function
Then Inuk is doing what you want.
i used v[:] but now the list is sorted
is that what happens with set.update ?
quick explanation of the v[:] syntax:
this is slice assignment, slices from a start and end are of format v[start:stop], if you omit start it means "from the start" and if you omit stop it means "to the end", so v[:]= means replace everything from start to finish
you mean unsorted? sets lose order
set.update does not do a sort. But you provided a sorted list anyway.
if you want to keep order, you can not directly turn a set into a list and use it
In recent Pythons they might preserve insert order like dicts.
The OP's original list above is already sorted, too.
def remove_duplicates(v):
s = set()
s.update(v)
v[:] = list(s)
v = [5,4,2,1,2,3,4,5]
remove_duplicates(v)
print(v)```
And what's it print?
[1,2,3,4,5]
any order you find when list-ifying a set is coincidental
Interesting. I notice those all occur in the last bit of your list. Try shuffling things around more.
Yeah, sets are not, of themselves, ordered.
s = set()
s.update(v)
v[:] = list(s)
v = [5,4,2,1,2,3,4,5,6,3,5,2,4,1,24,236,71]
remove_duplicates(v)
print(v)```
Rereading the OP's question, they might be required to preserve the numbers in the original list order, and discard later numbers in thelist.
So tossing them into a set and pulling out is not the way to go. Just use a set to keep a reference for what you've already seen.
list(dict.fromkeys(your_list))
To preserve order
yeah that's a good idea, a dict with dummy values is like a set but with order
Sets are unordered. Exploit dicts instead
(For the OP: dicts preserve their keys in the order they were inserted.)
and adding an already existing key to a dict will not move the existing key's position, so only the first added key when there are duplicates makes a difference in this case
so for
v = [5,4,2,1,2,3,4,5]
the first item in the result, when using the dict idea, will be 5
getting same output with v[:] = list(dict.fromkeys(s))
you're already using sets and destroying order
stop using sets
remember, sets = unordered
- i dont believe i should be using dict/the problem is encouraging me to use a set as auxiliary 2) i understand a set is destroying order, but how does it manage to sort the parameters in my previous code
the sorting you see is coincidental:
x = [539, 29, 677777, 399]
set(x)
{677777, 539, 29, 399}
pay no mind to it
Don’t reassign it.
Clear and extend
def remove_duplicates(v):
seen = set()
v[:] = [x for x in v if x not in seen and not seen.add(x)]
v = [3,3,3,2,2,2,1,1,1,9,9]
remove_duplicates(v)
print(v)
it's a slice assign, does the exact same as clear and extend
Oh yeah, mb
ok yeah i see now, thank you
you're supposed to use set to keep track of elements (membership checking for already seen items is more efficient than using a list), just like a spoonfed up above
Sorry
the code is very slightly obfuscated, i think OP gets the idea though and can make their own
ty everyone, python is new to me; didnt know parameters were mutable and syntax for for loops is different for me
should probably not do this if you don't understand how it works
i think the biggest part was not understanding how to change my parameter
i need to v[] not just v
cause v will just be a new variable?
a good thing to remember is that in python every variable is a reference to some value in memory, a pointer essentially, but because only some values are mutable it only matters for those mutable values
= means this variable points to a new value, essentially say this variable points to something else
so when you want to mutate a value, you use mutation methods, you can't use =
when you initialize v rather than v[:], the v is only changed locally in the function rather than the global v. You can print and return v but the global v will always be unchanged unless you use v[:]
v[:]= calls a mutation method (i can't remember which), while v= just changes what the variable points to
yeah i was thinking i could put all my elements from my list into a set, and then readding them to a list
i guess i could have used append, but the set would have a random order(?)
def remove_duplicates(v):
s = set()
for x in v:
if x not in s:
s.add(x)
v[].append(x)
doesnt work but
something like this ?
!eval
def remove_duplicates(v):
s = set()
i = 0
while i < len(v):
x = v[i]
if x not in s:
s.add(x)
v.append(x)
i += 1
# remove duplicates in the second part of the list
del v[len(v)//2:]
v = [1, 1, 1, 2, 2, 3, 3]
remove_duplicates(v)
print(v)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
[1, 1, 1, 2, 2]
ty everyone for their time!
could you explain 'and not see.add(x)'
does that result in a boolean?
like if you cant add x to a set it will return false?
firstly it checks to see if x is in the set, and then the and not seen.add(x) basically does two things at once, firstly it adds x to the set if it is not in it, and by default seen.add(x) returns None, and not None is always true, so its a sort of clever oneliner
we never have to worry about adding the same item to the set because of the line if x not in seen, and if that is true, the next line adds x to seen
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.