Python .... eliminate repeated data in a list

I have a list made up of 45 elements with a list of 3 elements inside each:
([0]-Sistemazioni …, [1] Point…….,[2] numero….) :

I need to eliminate double repeating Points. For this I create a list with only the points (resultantList1) which will still contain 45 elements with only the points. Double points can now be eliminated from this list:

for k in range (0, len(myList)):
** resultantList1.append(myList[k][1])**

But the same index k allows me to determine the list without duplications but made up of the 3 elements (([0]-Accomodations…, [1] Point…….,[2] number….).
Strangely, the list is not created with the index k:

rL.insert(j,[myList[k][0],myList[k][1],myList[k][2]])

appears empty
Some help
Thank you

Hi
if can help (unless you are looking for 100% python)


Sincerely
christian.stan

4 Likes

Hi Alessandro, welcome to the forum :wave:
You need to check you logic, the list resultantList1 has all the points - then on line 13 you check if a point from the list resultantList1[k] is not in the list - this will always be false, hence empty list
As I understand you would like all the lists, but no duplicate points. Let’s create a function, that checks for duplicates at a list index but returns the list

def list_unique_at_index(items, idx):
    unique_items = []
    output = []
    for i in items:
        if i[idx] not in unique_items:
            unique_items.append(i[idx])
            output.append(i)
    return output

Here is an example (using a bit of extra code)

points = [j for i in IN[0] for j in i]  # Flatten inputs
letters = "ABCDEF"
points = list(zip(letters, points))

OUT = points, list_unique_at_index(points, 1)

1 Like

Grazie Cristiano
bella soluzione… cerco anche quella in Python
Thanks Cristiano
nice solution… I’m also looking for that in Python

1 Like

Thanks Mike

You found my mistake, trivial.

Your path is excellent even if sentences like:

points = [j for i in IN[0] for j in i] # Flatten inputs

(you can find examples for these sentences?)

They find me unprepared but I understand them

I then corrected the error and continued on my way

1 Like

They are known as list comprehensions
The official python docs are here
You could also look here
Once you get the syntax they are very handy to do list processing and filtering

1 Like

hi,

a solution with pandas

import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

import pandas as pd
import numpy as np

datas = IN[0]
df = pd.DataFrame(np.array(datas), columns=['Name', 'Point', 'Value'])
df["PointRepr"] = df['Point'].apply(lambda x: x.ToString())
df = df.drop_duplicates(('PointRepr'))
df = df.drop(["PointRepr"], axis=1)
OUT = df.values.tolist()
2 Likes