-2

I'm new to Python. I want to print elements in the list without duplicate in a specific format: {"id": value}. Here is my code.

d =['l','o','u','l','b','l','a']

for item in range(0,len(d)):
  prev=item-1
  if item==0:
    print("  {\"id\": \"" + (d[item]) + "\"} ,") 
    continue
  if (d[item]==d[prev])or(d[item] in d[:prev]):
    continue
  else:
    print("  {\"id\": \"" + (d[item]) + "\"}"),
    if (item==(len(d))): #if last element, print  ".", else print ","
        print(".\n"),
    else:
        print(",\n"),

The result should be:

  {"id": "l"} ,
  {"id": "o"} ,
  {"id": "u"} ,
  {"id": "b"} ,
  {"id": "a"} .

But the output is:

  {"id": "l"} ,
  {"id": "o"} ,
  {"id": "u"} ,
  {"id": "b"} ,
  {"id": "a"} ,

My code works well until the last line. It ends with "," instead of ".". Please help me

4 Answers4

1

Try if (item==len(d)-1):

The last item has index len(d)-1.

Btw, there is way easier approaches to remove duplicates in a list using set():

list(set(['a', 'a', 'b'])) will return ['a','b'])

Warning: If you use set() you will loose the order of the items. If you want to keep the order, see How do you remove duplicates from a list in whilst preserving order?.

Community
  • 1
  • 1
mcb
  • 398
  • 2
  • 12
  • thanks. it works. but if I edit the list to d =['l','o','u','l','b','l'], it doesn't work. is there any mistake in my code? – Hoang-Viet Nguyen May 16 '17 at 11:18
  • This is because you do not enter the `else` clause. ` if d[item]==d[prev])or(d[item] in d[:prev]):` returns true because you have already seen 'l'. – mcb May 16 '17 at 11:21
  • Do we have any other ways? Because of some further problems, the list should be kept originally, not remove any elements inside. – Hoang-Viet Nguyen May 16 '17 at 14:52
  • Solved. I create a new list without no duplicate + preserving order. Then everything left is easy to solve. Thanks – Hoang-Viet Nguyen May 16 '17 at 16:08
0

In python the range(0, N) function returns numbers from 0 - N-1. In your case you have used N = len(d), so you should change the if statement accordingly. Try:

if (item==(len(d)-1)): #if last element, print  ".", else print ","
    print(".\n")

Instead.

The full running code should look like this:

d =['l','o','u','l','b','l','a']
for item_id, item in enumerate(set(d)):
  if item_id != (len(d) - 1):
    print("  {\"id\": \"" + item + "\"} ,") 
  else:
    print("  {\"id\": \"" + item + "\"} .")
Roy Jevnisek
  • 320
  • 1
  • 8
0

if (item==(len(d))) code will never hit this block, because item can only go upto len(d)-1

d =['l','o','u','l','b','l','a']

for item in range(0,len(d)):
  prev=item-1
  if item==0:
    print("  {\"id\": \"" + (d[item]) + "\"} ,") 
    continue
  if (d[item]==d[prev])or(d[item] in d[:prev]):
    continue
  else:
    print("  {\"id\": \"" + (d[item]) + "\"}"),
    if (item!=(len(d))-1): #if last element, print  ".", else print ","
        print(",\n"),
print(".\n")

Using sets:

d =['l','o','u','l','b','l','a']
d = list(set(d))
for item in range(0,len(d)):
    print("  {\"id\": \"" + (d[item]) + "\"}"),
    if (item!=(len(d))-1): #if last element, print  ".", else print ","
        print(",\n"),
print(".\n")
Pavan
  • 108
  • 1
  • 12
0

As range is exclusive, range(0,len(d)) will have values from 0 to len(d) - 1. So, your if statement should be -

if item == (len(d) - 1):
    print(".\n")

If the last element in the list is a duplicate it is better to create a set. Try the following -

d = {'l','o','u','l','b','l'}

for i, e in enumerate(d):
    print("  {\"id\": \"" + (e) + "\"}"),
    if i == len(d) - 1:
        print(".\n")
    else:
        print(",\n")

Here, d = {'l','o','u','l','b','l'} creates a set which removes duplicate elements. Then you can just simply iterate over it

kuro
  • 3,214
  • 3
  • 15
  • 31