1

I basically have a list of list like this:

[['1348559717', '11'],
 ['1348559717', '15'],
 ['1348562275', '16'],
 ['1348562275', '20'],
 ['1348562284', '17'],
 ['1348562284', '18'],
 ['1348562290', '19'],
 ['1349346149', '15'],
 ['1349348467', '14'],
 ['1350001260', '17']]

I would like to remove the lists which contains duplicated values in index [0]. Also, i always need to have the list which its index [1] = '20', in this case ['1348562275', '20']. So my wanted list would be:

[['1348559717', '11'],
 ['1348562275', '20'],
 ['1348562284', '17'],
 ['1348562290', '19'],
 ['1349346149', '15'],
 ['1349348467', '14'],
 ['1350001260', '17']]

Does anyone have any idea how could i do it?

3 Answers 3

1

You could use a dictionary then convert it back to a list after processing:

from pprint import PrettyPrinter

lst = [['1348559717', '11'],
       ['1348559717', '15'],
       ['1348562275', '16'],
       ['1348562275', '20'],
       ['1348562284', '17'],
       ['1348562284', '18'],
       ['1348562290', '19'],
       ['1349346149', '15'],
       ['1349348467', '14'],
       ['1350001260', '17']]

id_to_num = {}
for id_, num in lst:
    if id_ not in id_to_num or num == '20':
        id_to_num[id_] = num

new_lst = [[id_, num] for id_, num in id_to_num.items()]

PrettyPrinter().pprint(new_lst)

Output:

[['1348559717', '11'],
 ['1348562275', '20'],
 ['1348562284', '17'],
 ['1348562290', '19'],
 ['1349346149', '15'],
 ['1349348467', '14'],
 ['1350001260', '17']]
Sign up to request clarification or add additional context in comments.

2 Comments

Nice! That's what i was looking for. Btw, a question a bit off topic, why use pprint instead of print?
So it actually looks your expected output/is not all printed on one line. Like If you just use print the output won't have new lines between the elements.
1

You can probably optimize it more but here is the idea :

items = [['1348559717', '11'],
 ['1348559717', '15'],
 ['1348562275', '16'],
 ['1348562275', '20'],
 ['1348562284', '17'],
 ['1348562284', '18'],
 ['1348562290', '19'],
 ['1349346149', '15'],
 ['1349348467', '14'],
 ['1350001260', '17']]

    items_dict = {}
    for i in items:
      if i[0] not in items_dict or i[1] == '20':
        items_dict[i[0]] = i[1]
    items_list = []
    for k,v in items_dict.items():
      items_list.append([k, v])
    print(items_list)

Output :

[['1348559717', '11'],
 ['1348562275', '20'],
 ['1348562284', '17'],
 ['1348562290', '19'],
 ['1349346149', '15'],
 ['1349348467', '14'],
 ['1350001260', '17']]

Comments

1

How about use dict type like this?

some_list = [...] # your list what basically have 
some_dict = {item[0]: item[1] for item in some_list}

#always need to have the list which its index [1] = '20'
for k, v in [i for i in a if i[1] == '20']:
    some_dict[k] = v

some_list = [[k, v] for k, v in some_dict.items()]

Output:

[['1348559717', '15'], 
 ['1348562275', '20'], 
 ['1348562284', '18'], 
 ['1348562290', '19'], 
 ['1349346149', '15'], 
 ['1349348467', '14'], 
 ['1350001260', '17']]

I hope you can tell the rules about delete duplicated value in more detail.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.