How to get unique python list items

by Alex
How to get unique python list items

Suppose there is a list that contains repeating numbers:

numbers = [1, 1, 2, 3, 3, 4]

But we need a list with unique numbers:

numbers = [1, 2, 3, 4]

There are several ways to get unique values. Let’s look at them.

Option 1: Using a set to get elements

Using aset is one of the options. It is convenient in that it includes only unique elements. After that the set can be turned back into a list. Let’s look at two ways of using a set and a list. The first is quite detailed, but it allows you to see what is happening at each step.

numbers = [1, 2, 2, 3, 3, 4, 5]

def get_unique_numbers(numbers):
    list_of_unique_numbers = []
    unique_numbers = set(numbers)

   for numbers in unique_numbers:
        list_of_unique_numbers.append(number)

   return list_of_unique_numbers

print(get_unique_numbers(numbers))

Let’s look at what happens at each stage. We have a list of numbers . We pass it to the get_unique_numbers function. Inside this function, an empty list is created, which will eventually include all the unique numbers. After that, a set is used to get the unique numbers from the list of numbers.

unique_numbers = set(numbers)

The result is a list of unique numbers. It remains to make a list out of it. To do this, you can use a loop, going through each of the elements.

for numbers in unique_numbers:
       list_of_unique_numbers.append(number)

At each iteration, the current number is added to the list_of_unique_numbers. Finally, it is this list that is returned at the end of the program. There is a shorter way of using sets to get unique values in Python. That’s what we’ll talk about next.

The short version with set

All of the code above can be compressed into a single line using Python’s built-in functions.

numbers = [1, 2, 2, 3, 3, 4, 5]
unique_numbers = list(set(numbers))
print(unique_numbers)

Although this code is very different from the first example, the idea is the same. First, the set is used to get unique values. After that, the set is turned into a list.

unique_numbers = list(set(numbers))

It is easiest to think “from the inside out” when reading this code. The most nested code is executed first: set(numbers). Then the outside block: list(set(numbers)).

Option #2. Using a for loop

It’s also worth considering the loop approach. First, you need to create an empty list, which will include unique numbers. After that, you can use a loop to iterate over each number in the passed list. If a number from it is in the unique, you can move on to the next element. Otherwise, add that number. Let’s look at two ways to use a loop. Let’s start with the more detailed one.

numbers = [20, 20, 30, 30, 40]

def get_unique_numbers(numbers):
    unique = []

   for numbers in numbers:
       if number in unique:
           continue
       else:
            unique.append(number)
   return unique

print(get_unique_numbers(numbers))

Here is what happens in each step. First, there is a list of numbers . It is passed to the get_unique_numbers function. Within that function, an empty unique list is created. It will end up including all unique values. The loop will be used to search through the numbers in the numbers list.

   for number in numbers:
       if number in unique:
           continue
       else:
            unique.append(number)

Conditional constructions in the loop check if the number of the current iteration is in the list unique. If yes, the loop goes to the next iteration. If not, the number is added to the list. It is important to note that only unique numbers are added. When the loop is complete, the unique list with unique numbers is returned.

The short way with the loop

There is another way to use the cycle variant, which is shorter by a few lines.

numbers = [20, 20, 30, 30, 40]

def get_unique_numbers(numbers):
    unique = []
   for numbers in numbers:
       if number not in unique:
            unique.append(number)
   return unique

The difference is the conditional construct. This time it is as follows – if a number is not in unique, it must be added.

if number not in unique:
    unique.append(number)

Otherwise the loop goes to the next number in the list of numbers. The result will be the same. But sometimes this kind of thing is harder to read when a boolean value is omitted. There are several other ways to find unique values in a Python list. But the ones described in this article will suffice.

Related Posts

LEAVE A COMMENT