0

I have a problem concerning duplicate list entries. My data are more complex than the below example, but the idea is the same.

I have a list. Depending on other factors (outside this code), in some cases I need a duplicate of some of the list elements. Having copied them, I then want to give all list elements a unique number. However, I find that the copied elements (the original and the copy) get the same number. How do I solve this problem?

list = [["apple", '', "a"], ["pear", '', "b"]] 
list.append(list[0])

counter = 0 
for item in list:  
    counter += 1
    item[1] = counter

This yields the list:

[['apple', 3, 'a'], ['pear', 2, 'b'], ['apple', 3, 'a']]

But what I wanted was:

[['apple', 1, 'a'], ['pear', 2, 'b'], ['apple', 3, 'a']]

(I can't just append the counter, because it's a specific column that needs to be filled in)

fredtantini
  • 15,966
  • 8
  • 49
  • 55
Johanna
  • 1,019
  • 2
  • 9
  • 20

0 Answers0