find and update duplicates in a list of lists
Question: I am looking for a Pythonic way to solve the following problem. I have (what I
think is) a working solution but it has complicated flow controls and just
isn't "pretty". (Basically, a C++ solution)

I have a list of lists. Each list contains multiple items of varying types
(maybe 10 items per list) The overall order of the lists is not relevant, but
the order of the items in any individual list is important. (ie I can't change
it).

I am looking to "tag" duplicates by adding an extra field to the end of an
individual list. However, in this case a "duplicate" list is one that has
equal values in several preselected fields, but not all fields (there are no
"true" duplicates).

For example: if this were the original data from a 5 item list of lists and
duplicate is defined as having equal values in the first and third fields:

    
    
    ['apple', 'window', 'pear', 2, 1.55, 'banana']
    ['apple', 'orange', 'kiwi', 3, 1.80, 'banana']
    ['apple', 'envelope', 'star_fruit', 2, 1.55, 'banana']
    ['apple', 'orange', 'pear', 2, 0.80, 'coffee_cup'] 
    ['apple', 'orange', 'pear', 2, 3.80, 'coffee_cup']
    

The first, fourth and fifth lists would be duplicates and therefore all lists
should be updated as follows:

    
    
    ['apple', 'window', 'pear', 2, 1.55, 'banana', 1]
    ['apple', 'orange', 'kiwi', 3, 1.55, 'banana', 0]
    ['apple', 'envelope', 'star_fruit', 2, 1.55,'banana', 0]
    ['apple', 'orange', 'pear', 2, 3.80, 'coffee_cup', 2]  
    ['apple', 'orange', 'pear', 2, 3.80, 'coffee_cup', 3]
    

Thanks for any help or direction. I think this may be getting beyond the
Learning Python book.


Answer: 
    from collections import defaultdict
    
    lists = [['apple', 'window', 'pear', 2, 1.55, 'banana'],
    ['apple', 'orange', 'kiwi', 3, 1.80, 'banana'],
    ['apple', 'envelope', 'star_fruit', 2, 1.55, 'banana'],
    ['apple', 'orange', 'pear', 2, 0.80, 'coffee_cup'],
    ['apple', 'orange', 'pear', 2, 3.80, 'coffee_cup']]
    
    dic = defaultdict(int)
    fts = []
    for lst in lists:
        first_third = lst[0], lst[2]
        dic[first_third] += 1
        if dic[first_third] == 2: fts.append(first_third)
        lst.append(dic[first_third])
    
    for lst in lists:
        if (lst[0], lst[2]) not in fts:
            lst[-1] -= 1
    
    print(lists)
    

Edit: Thanks utdemir. `first_third = lst[0], lst[2]` is correct, not
`first_third = lst[0] + lst[2]`

Edit2: Changed variable names for clarity.

Edit3: Changed to reflect what the original poster really wanted, and his
updated list. Not pretty any more, desired changes just tacked on.

