• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Create Unique List from List in Python

Create Unique List from List in Python

August 15, 2022 Leave a Comment

To get the unique items of a list in Python, the easiest way is by converting the list to a set with set() and then back to a list with list().

l = [0,7,7,7,0,2,3,1,1,4,5,6,7]

l_unique = list(set(l))

print(l_unique)

#Output:
[0,1,2,3,4,5,6,7]

If need to preserve the order of the elements in the list, then you can use comprehension to obtain a unique list.

l = [0,7,7,7,0,2,3,1,1,4,5,6,7]

l_unique = []

[l_unique.append(x) for x in l if x not in l_unique]

print(l_unique)

#Output:
[0, 7, 2, 3, 1, 4, 5, 6]

When working with collections of data in Python, the ability to get the unique values of your data is valuable.

To get the unique values of a list in Python, the easiest way is by converting the list to a set with set() and then back to a list with list().

A set is an unordered collection of unique elements. On the other hand, lists are ordered and can contain duplicates.

Converting a list to a set creates a set with the same items as the list and returns a unique list.

Below is how to get the unique values from a list with the set() function in Python.

l = [0,7,7,7,0,2,3,1,1,4,5,6,7]

l_unique = list(set(l))

print(l_unique)

#Output:
[0,1,2,3,4,5,6,7]

Obtaining Unique Values of List and Preserving Order of Elements in Python

If need to preserve the order of the elements in the list, then we need to do a little more work.

When converting a list to a set, the duplicates of the list are removed and also sorted.

To preserve the order of the elements in the list, you need to use a loop. To loop in Python, you can use comprehension or a standard for loop.

The process here to preserve the order of the elements of the original list is to create a second list.

We will append items to the second list if they aren’t already in the second list and this will preserve the order of the original list.

Below shows you how to obtain a unique list of values from a list and preserve the order of the elements with comprehension in Python.

l = [0,7,7,7,0,2,3,1,1,4,5,6,7]

l_unique = []

[l_unique.append(x) for x in l if x not in l_unique]

print(l_unique)

#Output:
[0, 7, 2, 3, 1, 4, 5, 6]

This is the same as the following for loop.

l = [0,7,7,7,0,2,3,1,1,4,5,6,7]

l_unique = []

for x in l:
    if x not in l_unique: 
        l_unique.append(x)

print(l_unique)

#Output:
[0, 7, 2, 3, 1, 4, 5, 6]

Hopefully this article has been useful for you to create a unique list from a list in Python.

Other Articles You'll Also Like:

  • 1.  pandas cumprod – Find Cumulative Product of Series or DataFrame
  • 2.  Find Maximum of Three Numbers in Python
  • 3.  Create Empty List in Python
  • 4.  How to Slice a Dictionary in Python
  • 5.  How to Check if a String Contains Vowels in Python
  • 6.  Creating a Random Color Turtle in Python
  • 7.  Using Python to Remove Duplicates from List
  • 8.  Python Get Operating System Information with os and platform Modules
  • 9.  Add Key Value Pair to Dictionary in Python
  • 10.  How to Create Block and Multi-Line Comments in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy