Diwakar_Sharma08 Diwakar_Sharma08 - 6 months ago 10
Python Question

Deleting similar elements from a list in python

I want to delete similar elements from a list in Python and print uniques element in that list without using set(). However my code is not doing the same . please help me out

My code

T = int(raw_input())
a=[]
for i in range(0,T):
a.append(raw_input())
for i in range(len(a)):
for j in range(i+1, len(a)):
if a[i]==a[j]:
del a[j]
print a

Answer

Use remove:

T = int(raw_input())
a=[]

for i in range(0,T):
    a.append(raw_input())


for i in range(len(a)):
    for j in range(i+1, len(a)):
        if a[i]==a[j]:
            a[j]=''


while '' in a:
    a.remove('')

print a 

try my solution , rather than deleting an element you can make it and empty string and than remove it , as deleting would cause your list index to go out of range