Create a new list by combining duplicate elements in the list

How to change ["a", "a", "a", 2,3,3,2,2] to ['a', 2,3].

def remove_duplicates(x):
    list_a = []
    for index,i in enumerate(x):
        list_a.append(i)
        if list_a.count(i) > 1:
            list_a.remove(i)
    return list_a
print remove_duplicates(["a","a","a",2,3,3,2,2])
#['a',2,3]

Or rather, it seems that the following was good.

def remove_duplicates(x):
    y=[]
    for i in x:
        if i not in y:
            y.append(i)
    return y

Recommended Posts

Create a new list by combining duplicate elements in the list
Get only the subclass elements in a list
Group by consecutive elements of a list in Python
Get the number of specific elements in a python list
Change the list in a for statement
Make a copy of the list in Python
Create a layout by combining Dash and Bootstrap
Create a new page in confluence with Python
Search by the value of the instance in the list
Until you create a new app in Django
Sort list elements in a specified order in Python
[Python] Manipulating elements in a list (array) [Sort]
How to remove duplicate elements in Python3 list
Create a BOT that displays the number of infected people in the new corona
[Python] Outputs all combinations of elements in the list
Create a dictionary by searching the table using sqlalchemy
How to check in Python if one of the elements of a list is in another list
Make sure all the elements in the list are the same in Python
Create a new csv with pandas based on the local csv
Create a function in Python
Create a dictionary in Python
Create a local scope in Python without polluting the namespace
Search list for duplicate elements
Create a list in Python with all followers on twitter
Get and create nodes added and updated in the new version
Sort and output the elements in the list as elements and multiples in Python.
How to create a record by pasting a relation to the inheriting source Model in the Model inherited by Django
How to get the last (last) value in a list in Python
Methods available in the list
Parse the Researchmap API in Python and automatically create a Word file for the achievement list
Getting list elements in Python
Output the key list included in S3 Bucket to a file
[Python] Display only the elements of the list side by side [Vertical, horizontal]
Read the standard output of a subprocess line by line in Python
Create a pandas Dataflame by searching the DB table using sqlalchemy
Determine if all list elements are present in the dict key
Dig the directory and create a list of directory paths + file names
Create a function to get the contents of the database in Go
[python] Manage functions in a list
Create a CSV reader in Flask
Create a DI Container in Python
Create a binary file in Python
Create a Kubernetes Operator in Python
Delete multiple elements in python list
When creating a matrix in a list
Create a random string in Python
Create a LINE Bot in Django
Create a bot that posts the number of people positive for the new coronavirus in Tokyo to Slack
A memo organized by renaming the file names in the folder with python
Generate a list packed with the number of days in the current month.
Is the space replaced by a plus sign or% 20 in percent-encoding processing?
[Linux] Command to get a list of commands executed in the past
Test & Debug Tips: Create a file of the specified size in Python
I want to sort a list in the order of other lists
Receive a list of the results of parallel processing in Python with starmap
Create a record with attachments in KINTONE using the Python requests module
What is the fastest way to create a reverse dictionary in python?
How to sort by specifying a column in the Python Numpy array.
Even a new graduate engineer can do it! The trick to make a difference in synchronization by commenting out
Create a new Python numerical calculation project
Write the test in a python docstring