-1
unknown_list = ["toby", "James", "kate", "George", "James", "rick", "Alex", "Jein", "Alex", "Alex", "George", "Jein", "kate", "medelin"]

for i in range(len(unknown_list)):
    item = unknown_list[i]
    unknown_list[i] = item.replace(item[0], item[0].upper())

x = 0
for b in range(0, len(unknown_list)):
    if unknown_list[b] in unknown_list[b+1:]:
        unknown_list.remove(unknown_list[b])
        x += 1


print(x)
print(unknown_list)

I'm so stuck. There's error in line 9. I need to remove duplicates from list (I can't use function that I haven't learnt yet, that's why I write code like that)

dpant
  • 1,622
  • 19
  • 30
  • 1
    The advice is NOT to delete elements from the list you are looping over. You need to create another list and append non-duplicates there. Also, please, write the actual task in description – Olia Nov 05 '19 at 18:54

4 Answers4

1

This is a simpler way to do it. There is a built in function called set

print(list(set(unknown_list)))

output:
['Jein', 'Alex', 'James', 'toby', 'George', 'rick', 'medelin', 'kate']

refer : https://www.geeksforgeeks.org/sets-in-python/

roshan ok
  • 383
  • 1
  • 6
0

It's a one-liner: list(set(source_list)) will do the trick.

A set is something that can't possibly have duplicates.

0

Your index error is occuring because in a list of n items, there are n-1 indicies (since we start at list[0])

unknown_list = ["toby", "James", "kate", "George", "James", "rick", "Alex", "Jein", "Alex", "Alex", "George", "Jein", "kate", "medelin"]

for i in range(len(unknown_list)):
    item = unknown_list[i]
    unknown_list[i] = item.replace(item[0], item[0].upper())

a=1

for b in range (0, len(unknown_list)-1):
    if unknown_list[b] in unknown_list[a:]:
        unknown_list.remove(unknown_list[b])
        a +=1

print(unknown_list)

You also have x = 0 which you never use, though you iterate up on it. However also, this will remove all the names from the list and not leave the first (so you're left with only names that appeared in the list once).

A way to do this where you end up with a list of all the unique names is to create an empty list and then append distinct names:

unknown_list = ["toby", "James", "kate", "George", "James", "rick", "Alex", "Jein", "Alex", "Alex", "George", "Jein", "kate", "medelin"]

for i in range(len(unknown_list)):
    item = unknown_list[i]
    unknown_list[i] = item.replace(item[0], item[0].upper())

distinct =[]

for i in unknown_list: 
    if i not in distinct: 
        distinct.append(i) 
datadumn
  • 45
  • 9
0

This should work:

unknown_list = ["toby", "James", "kate", "George", "James", "rick", "Alex", "Jein", "Alex", "Alex", "George", "Jein", "kate", "medelin"]

for i in range(len(unknown_list)):
    unknown_list[i] = unknown_list[i].upper()

print (unknown_list)

i = 0
n = len(unknown_list)
while i < n:
    item = unknown_list[i]
    if item in unknown_list[i+1:]:
        unknown_list.remove(item)
        n = n - 1
    else:
        i = i + 1

print (unknown_list)

Explanation:

Never remove elements from a list inside a for loop. Never. You'll almost certainly end up with the index out of range error. And even if you don't at least half the elements will be skipped.

One solution is to use a while loop. Since the elements of the list are shifted to the left upon removal the trick is to only advance the i index when no element is removed. On the other hand, if an element is removed you stay put (the unknown_list[i] element will now be the shifted element to compare) and, of course, you decrease the total number of elements in the list (n).

Please, make sure you understand what happens to the list after an item is removed. You can figure it out by looking at the order of the elements in the resulting list:

['TOBY', 'JAMES', 'RICK', 'ALEX', 'GEORGE', 'JEIN', 'KATE', 'MEDELIN']

compared to the input list:

['TOBY', 'JAMES', 'KATE', 'GEORGE', 'JAMES', 'RICK', 'ALEX', 'JEIN', 'ALEX', 'ALEX', 'GEORGE', 'JEIN', 'KATE', 'MEDELIN']

dpant
  • 1,622
  • 19
  • 30