print all the unique values in nl10107 (no duplication) in python

You can follow these steps to print all the unique values in a list in Python:

  1. Define the list with the values.
  2. Convert the list to a set to remove duplicates.
  3. Convert the set back to a list.
  4. Print the list.

Here's the code:

main.py
nl10107 = [1, 2, 2, 3, 4, 5, 5, 6, 6, 6, 7, 8, 8]
unique_nl10107 = list(set(nl10107))
print(unique_nl10107)
108 chars
4 lines

In the code above, the set() function will create a set with only the unique values from the list, and the list() function will convert this set into a new list with only unique values.

Output:

main.py
[1, 2, 3, 4, 5, 6, 7, 8]
25 chars
2 lines

Alternatively, you can use a for loop and a conditional statement to check for duplicates and print only unique values:

main.py
nl10107 = [1, 2, 2, 3, 4, 5, 5, 6, 6, 6, 7, 8, 8]
unique_nl10107 = []

for num in nl10107:
    if num not in unique_nl10107:
        unique_nl10107.append(num)

print(unique_nl10107)
183 chars
9 lines

Output:

main.py
[1, 2, 3, 4, 5, 6, 7, 8]
25 chars
2 lines

Both options will produce the same output, but the first one is more concise and efficient.

related categories

gistlibby LogSnag