Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Printing indices of list containing different elements in Python

I have a list X. I want to print indices of X which contain different list elements. For instance, X[0] has some different elements, X[1] has all the same elements and X[2] has again different elements. I present the expected output.

X=[[[46.36363636363636],
 [35.90909090909091],
 [31.363636363636367],
 [25.454545454545453],
 [24.545454545454547],
 [24.545454545454547],
 [24.545454545454547],
 [24.545454545454547],
 [24.545454545454547],
 [24.545454545454547]],

   [[46.36363636363636],
 [46.36363636363636],
 [46.36363636363636],
 [46.36363636363636],
 [46.36363636363636],
 [46.36363636363636],
 [46.36363636363636],
 [46.36363636363636],
 [46.36363636363636],
 [46.36363636363636]],
    
   [[46.36363636363636],
    [39.09090909090909],
    [37.27272727272727],
    [35.0],
    [33.18181818181819],
    [32.72727272727273],
    [32.27272727272727],
    [32.27272727272727],
    [32.27272727272727],
    [32.27272727272727]]]

for i in range(0,len(X)):
    X = [x for x in X if min(x) != max(x)]

The expected output is

i=[0,2]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Loop over every list in your list, and check if each list contains only the same elements. If they don’t, return the index of the list.

res = [x for x, y in enumerate(X) if not all(e == y[0] for e in y)]
print(res)

>>> [0, 2]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading