#🔒 modifying parameter?

84 messages · Page 1 of 1 (latest)

echo terrace
#

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

buoyant totemBOT
#

@echo terrace

Python help channel opened

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.

snow magnet
#

Could you paste your code snippet here?

echo terrace
#

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)

buoyant totemBOT
#

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.
snow magnet
#

Also, paste the call

echo terrace
snow magnet
#

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.

hybrid bay
#

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)

fathom thorn
#

or the OP could just return the new list instead of modifying in place.

echo terrace
#

requirements to modify in the function

fathom thorn
#

Then Inuk is doing what you want.

echo terrace
#

is that what happens with set.update ?

hybrid bay
#

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

hybrid bay
fathom thorn
#

set.update does not do a sort. But you provided a sorted list anyway.

hybrid bay
#

if you want to keep order, you can not directly turn a set into a list and use it

fathom thorn
#

In recent Pythons they might preserve insert order like dicts.

#

The OP's original list above is already sorted, too.

echo terrace
#
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)```
fathom thorn
#

And what's it print?

echo terrace
#

[1,2,3,4,5]

hybrid bay
#

any order you find when list-ifying a set is coincidental

fathom thorn
#

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.

echo terrace
#
    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)```
fathom thorn
#

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.

snow magnet
#

list(dict.fromkeys(your_list))

echo terrace
snow magnet
hybrid bay
snow magnet
#

Sets are unordered. Exploit dicts instead

fathom thorn
#

(For the OP: dicts preserve their keys in the order they were inserted.)

hybrid bay
#

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

echo terrace
#

getting same output with v[:] = list(dict.fromkeys(s))

hybrid bay
#

stop using sets

#

remember, sets = unordered

echo terrace
#
  1. 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
hybrid bay
#

pay no mind to it

snow magnet
#

Clear and extend

crisp sapphire
#
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)
hybrid bay
snow magnet
#

Oh yeah, mb

hybrid bay
#

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

crisp sapphire
#

Sorry

hybrid bay
#

the code is very slightly obfuscated, i think OP gets the idea though and can make their own

echo terrace
#

ty everyone, python is new to me; didnt know parameters were mutable and syntax for for loops is different for me

crisp sapphire
echo terrace
#

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?

hybrid bay
hybrid bay
#

so when you want to mutate a value, you use mutation methods, you can't use =

crisp sapphire
# echo terrace i need to v[] not just v

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[:]

hybrid bay
#

v[:]= calls a mutation method (i can't remember which), while v= just changes what the variable points to

echo terrace
#

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 ?

crisp sapphire
#

!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)
buoyant totemBOT
crisp sapphire
#

op

#

sorry testing

echo terrace
#

ty everyone for their time!

echo terrace
#

does that result in a boolean?

#

like if you cant add x to a set it will return false?

crisp sapphire
# echo terrace could you explain 'and not see.add(x)'

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

buoyant totemBOT
#
Python help channel closed

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.