#๐Ÿ”’ duplicated list

32 messages ยท Page 1 of 1 (latest)

smoky hedge
#

list = [1,2,3,4,5,6,6,6]

un_duplicated_list = []

while list:
for i in list:
if list.count(i) > 1 and not un_duplicated_list:
un_duplicated_list.append(i)
list.remove(i)
print(list)

I am lost. I want to remove duplicated values from list and I want to keep what was duplicated in another list. like second list should be [6,6]

Please dont write completely different code just show me how can I fix on this.

ty

clever cypressBOT
#

@smoky hedge

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.

smoky hedge
#

what do u mean

bright oracle
#

like in a school project where some u cant use sum()

#

anything like that going on here ?

smoky hedge
#

no but I am trying to understand why I made this code wrong

#

so I can fix it

bright oracle
#

this will only be true when un_duplicated_list is empty

#

which it will never be after first iteration since u appended to it

smoky hedge
#
list = [1,2,3,4,5,6,6,6]

un_duplicated_list = []

while list:
 for i in list:
    if list.count(i) > 1:
        un_duplicated_list.append(i)
        list.remove(i)
        print(list)

clever cypressBOT
#

Hey @smoky hedge!

Please edit your message to use a code block

Add a py after the three backticks.

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
smoky hedge
#

didnt fix anyway

bright oracle
#

!e
list = [1,2,3,4,5,6,6,6]

un_duplicated_list = []

while list:
for i in list:
if list.count(i) > 1:
un_duplicated_list.append(i)
list.remove(i)
print(list)

clever cypressBOT
bright oracle
#

infinite loop

smoky hedge
#

I know so ?

bright oracle
#

!e

list = [1,2,3,4,5,6,6,6]

un_duplicated_list = []

while list:
 print(un_duplicated_list)
 for i in list:
    if list.count(i) > 1:
        un_duplicated_list.append(i)
        list.remove(i)
        print(list)
clever cypressBOT
ornate shore
bright oracle
#

did i forget to say that

#

i did huh

#

oops

smoky hedge
ornate shore
smoky hedge
#

Remove function will not make slips

glossy ferry
#

even though removing from a list while iterating through it in reverse might work, it gets confusing really quick

#

try doing it in separate loops first, and then when you get that right, take a shot at it again

smoky hedge
#

!close

clever cypressBOT
#
Python help channel closed with !close

This help channel has been closed. 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.