# \[SOLVED\] How to get a unique list from a bigger list of lists based on 1 criteria

**URL:** <https://forum.inductiveautomation.com/t/solved-how-to-get-a-unique-list-from-a-bigger-list-of-lists-based-on-1-criteria/37987>\
**Category:** General Discussion\
**Created:** [August 22, 2020, 12:25am UTC](https://forum.inductiveautomation.com/t/solved-how-to-get-a-unique-list-from-a-bigger-list-of-lists-based-on-1-criteria/37987 "2020-08-22T00:25:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [August 22, 2020, 12:25am UTC](https://forum.inductiveautomation.com/t/solved-how-to-get-a-unique-list-from-a-bigger-list-of-lists-based-on-1-criteria/37987/1 "2020-08-22T00:25:27Z")

</div>

Good evening,

I have an m by n table with a column called p. How can I get all 1 row per unique p value into a new table i by j?

Example:

```
  a b c p d
  1 2 1 1 'aab'
  1 1 1 2 'aac'
  3 3 1 2 'aad'
  3 2 2 3 'aag'

```

would become:

```
  a b c p d
  1 2 1 1 'aab'
  1 1 1 2 'aac'
  3 2 2 3 'aag'

```

I’ve broken down the first table into a list of lists but I can’t come up with a method for deleting the 3rd row in the table to get the new table.

Any help is much appreciated.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [August 22, 2020, 3:02am UTC](https://forum.inductiveautomation.com/t/solved-how-to-get-a-unique-list-from-a-bigger-list-of-lists-based-on-1-criteria/37987/2 "2020-08-22T03:02:13Z")

</div>

```python
listIn = [[1, 2, 1, 1, 'aab'],
          [1, 1, 1, 2, 'aac'],
          [3, 3, 1, 2, 'aad'], 
          [3, 2, 2, 3, 'aag']]

existsList =[]

listOut =[]

for row in listIn
	if row[3] not in existsList:
		existsList.append(row[3])
		listOut.append(row)

print listOut

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 22, 2020, 1:29pm UTC](https://forum.inductiveautomation.com/t/solved-how-to-get-a-unique-list-from-a-bigger-list-of-lists-based-on-1-criteria/37987/3 "2020-08-22T13:29:17Z")

</div>

If you are running with very large datasets, use a `set` or `dict` instead of a `list` for tracking existence.

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [August 22, 2020, 4:17pm UTC](https://forum.inductiveautomation.com/t/solved-how-to-get-a-unique-list-from-a-bigger-list-of-lists-based-on-1-criteria/37987/4 "2020-08-22T16:17:27Z")

</div>

```
output = []
seen = set()
for keyID, line, verName, modelDescrip, menGrp, staNo, seq, workCell, t_Sec, mCode, description, key_Point, quality_Chk, partNo, partName, scan, scan_req, pic, videoFileName, userAck in tbl:
   if seq in seen:
       continue
   output.append([keyID, line, verName, modelDescrip, menGrp, staNo, seq, workCell, t_Sec, mCode, description, key_Point, quality_Chk, partNo, partName, scan, scan_req, pic, videoFileName, userAck])
   seen.add(seq)

```

A user on Stackoverflow was able to come up with the above code but I like yours better and will probably implement it at some point this weekend.

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [August 22, 2020, 4:19pm UTC](https://forum.inductiveautomation.com/t/solved-how-to-get-a-unique-list-from-a-bigger-list-of-lists-based-on-1-criteria/37987/5 "2020-08-22T16:19:07Z")

</div>

At the end of the day I don’t think it will be ever going over 100 rows but there is no rule stating they can’t have 10k rows i guess.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 22, 2020, 7:37pm UTC](https://forum.inductiveautomation.com/t/solved-how-to-get-a-unique-list-from-a-bigger-list-of-lists-based-on-1-criteria/37987/6 "2020-08-22T19:37:02Z")

</div>

The code from Stackoverflow is using tuple unpacking to place the data into named variables. While handy if you have complex expressions to do, it is pointless in your case. I do like the use of continue for loop short-circuit and the use of the set. I would blend the two implementations.

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [August 24, 2020, 3:06pm UTC](https://forum.inductiveautomation.com/t/solved-how-to-get-a-unique-list-from-a-bigger-list-of-lists-based-on-1-criteria/37987/7 "2020-08-24T15:06:38Z")

</div>

Took me a while to understand what you meant about unpacking tuples but after reading about Python collections (arrays), Ignition Datasets, and Ignition PyDataSets I have a better understanding of how all the pieces fit together. Thanks for the insight.

```
output = []
seen = set()
		
for row in tbl:
    if row['seq'] in seen:
        continue
    output.append(list(row))
    seen.add(row['seq'])
```
