Removing duplicates from list of lists in Python

后端 未结 5 1467
温柔的废话
温柔的废话 2020-12-02 19:40

Can anyone suggest a good solution to remove duplicates from nested lists if wanting to evaluate duplicates based on first element of each nested list?

The main list

5条回答
  •  -上瘾入骨i
    2020-12-02 20:15

    use a dict instead like so:

    L = {'14': ['65', 76], '2': ['5', 6], '7': ['12', 33]}
    L['14'] = ['22', 46]
    

    if you are receiving the first list from some external source, convert it like so:

    L = [['14', '65', 76], ['2', '5', 6], ['7', '12', 33], ['14', '22', 46]]
    L_dict = dict((x[0], x[1:]) for x in L)
    

提交回复
热议问题