Python unique list order

How to get unique values from a list in Python

In this Python tutorial, we will learn how to get unique values from a list in Python. Also, we will cover these topics.

  • Get unique values from list Python Pandas
  • Get unique values from list Python
  • Get unique values from nested list Python
  • Get unique values from list of dictionary Python
  • Python get unique values from list of objects
  • Remove unique values from list Python
  • Get unique values from two list Python
  • Get unique value from list Python using index
  • Get unique values from list of lists Python
  • Get unique values from list of strings Python

Python Get unique values from list

  • In this section, we will discuss how to get unique values from a list by using Python.
  • In Python, the list is a number of elements and stored within [] brackets. When we are inserting elements in the list there must be duplicate values available in the list. So now our task is we have to find the unique values in a given list.
  • For example, suppose we have a Python list that contains integer numbers [5,6,4,5]. Now we have to find the unique values as you can see in the given list ‘5’ comes two times.
  • There are various method can be used to get unique values from a list by using Python.
    • By using set() method
    • By using ordered dict()
    • By using enumerator() method

    By using set() method

    • In Python the set method stores only unique values it will automatically remove duplicate values from the list when we apply the function ‘set()’. In python, the set is an unordered collection of elements and we can easily remove or update value from it.
    new_lis = [14,25,87,14,78,25,25] new_val = set(new_lis) z=list(new_val) print("Unique values:",z)

    In the above example, we created a list ‘new_lis’ and assign integers values to it. Then we extract the unique values from the list by applying the set() function.

    Now declare a variable ‘z’ and use the list() function. Once you will print ‘z’ then the output will display the unique values.

    Here is the implementation of the following given code

    Python Get unique values from list

    By using ordered dict()

    • Here we can easily use the ordered.dict() method from the collection module by using Python. In this example we have to create a list ‘new_values’ and then use OrderedDict.fromkeys() method with from keys() function.
    from collections import OrderedDict new_values = [16, 11, 11, 10, 45, 98, 52, 41] new_result = list(OrderedDict.fromkeys(new_values)) print("Unique values:",new_result)

    Here is the Output of the following given code

    Python Get unique values from list

    By using enumerator() method

    In Python, the enumerator() method is used to iterate objects and insert counter items. This is a built-in function in Python and the object can be used in loops and converted into the list.

    Source Code:

    new_val = [24,89,75,89,24,189,75,178] output = [i for z,i in enumerate(new_val) if i not in new_val[:z]] print("Unique value:",output) 

    In the above program, we initialized the list that contains integer and duplicate values. Now use the enumerator() function and within this method pass list comprehension as an argument that will return a unique value list.

    Here is the implementation of the following given code

    Python Get unique values from list

    Get unique values from list Python Pandas

    • In this Program, we will discuss how to get the unique values from list by using Python Pandas.
    • By using the pd.drop_duplicates() function we can solve this problem. To do this task we are going to import the Pandas library and then apply the pd.series() that is capable of storing various data types. we can also convert the list into series by using the ‘Series’ method.
    • In Python Pandas the drop_duplicate() function is used for removing duplicate values from series and returning the unique values.

    Here is the Syntax of drop_duplicate() function

    Dataframe.drop_duplicates ( subset=None, keep='first', inplace='False', ignore_index=False )
    import pandas as pd new_lis = [14, 67, 23, 67, 164, 754, 943, 14] result= pd.Series(new_lis).drop_duplicates().tolist() print("Unique values:",result)

    You can refer to the below Screenshot

    Get unique values from list Python Pandas

    As you can see in the Screenshot the Output displays the unique values from the given list.

    Get unique values from list Python

    • In this section, we will discuss how to find the unique values from list by using Python.
    • To perform this particular task we are going to use the concept of dict.fromkeys() method. In Python the dict.fromkeys() method is used to declare a new dictionary from the given values. Now we have to remove duplicate values from a list and store the unique values into the list.
    • By using the list() method we can easily get the new list and assign ‘new_result’ as an argument.

    Here is the Syntax of dict.fromkeys() method

    dictionary.fromkeys ( sequence [, value ] )
    • It consists of a few parameters
      • sequence: This parameter indicates the list of elements which is to be used.
      • value: It is an optional parameter and it specifies the each value of the dictionary.

      Let’s take an example and check how to get unique values from a list of Python

      new_values = [45,67,15,67,23,15] new_result = dict.fromkeys(new_values) b= list(new_result) print("Unique values:",b)

      Here is the Screenshot of the following given code

      Get unique values from list Python

      Get unique values from nested list Python

      • Here we can see how to get the unique values from nested list by using Python.
      • By using the frozenset() method we can easily get the unique values from a nested list. In Python, the frozenset() is used to return an immutable object. While the element of the set can be modified anytime and it will check the condition if the argument is provided then frozenset is created.

      Here is the Syntax of frozenset() method

      • It consists of only one parameter
        • iterable: This Parameter indicates the iterable object like lists.

        Let’s take an example and understand the working of the frozenset() method

        new_list=[[43,14],[784,821],[77,65],[65,77],[145,765],[43,14]] new_val = set(frozenset(i) for i in new_list) result = [list(i) for i in new_val] print("Unique values from list:",result)

        Here is the Screenshot of the following given code

        Get unique values from nested list Python

        Get unique values from list of dictionary Python

        • In this section, we will discuss how to get the unique values from list of dictionary by using Python.
        • To perform this particular task we are going to use the list function and within this method we are going to apply list comprehension method as an argument.

        Source Code:

        new_dict = [, , ] final_output = list(set(value for dic in new_dict for value in dic.values())) print("Unique values:",final_output)

        In the above code, we created a list of dictionaries and then initialized a variable ‘final_output‘ in which we have assigned the list values it can iterate over the dictionary values.

        Now convert to a list by using the dic.values() method. Once you will print ‘final_output’ then the result will display the unique value.

        Here is the implementation of the following given code

        Get unique values from list of dictionary Python

        Python get unique values from list of objects

        • In this section, we will discuss how to get the unique values from a list of objects by using Python.
        • By using the Python set() method we can easily perform this particular task. In Python the set method stores only unique values it will automatically remove duplicate values from the list.
        • Now to do this task first, we will create a list and assign integer and string values to it. After that declare a variable ‘result’ in which we are going to apply the set() method and within this function, we will pass list ‘new_values’ as an argument.
        new_values = [67,117,67,'b','z','b',25] result = set(new_values) z=list(result) print("Unique values:",z)

        You can refer to the below Screenshot

        Python get unique values from list of objects

        As you can see in the Screenshot the output displays the unique values from list objects

        Remove unique values from list Python

        • In this Program, we will discuss how to remove unique values from list by using Python.
        • In this example we are going to use the concept of list comprehension and count method. In Python the count() method is used to return the number of times objects that occurs in the list.

        Here is the Syntax of Python List count() method

        Source Code:

        new_list=[46,21,69,69,14,76,13,13,66,19,10,10] final_output=[i for i in new_list if new_list.count(i)>1] print("Remove unique values from list:",final_output)

        Here is the execution of the following given code

        Remove unique values from list Python

        As you can see in the Screenshot the output displays the updated list

        Get unique values from two list Python

        • Here we can see how to get unique values from two list by using Python.
        • To solve this problem we are going to apply the set() function. In Python, the set() function is used to get the unique values from given lists.
        • In this Program, we will create two lists ‘lis1’ and ‘lis2’. After that, we are going to declare a variable ‘z’ and ‘m’ in which we have assigned a list and set a function for getting the unique values.
        lis1 = [23,41,23,78,94,41] lis2 = [78,109,78,56,234,109] z=list(set(lis1) - set(lis2)) m=list(set(lis2) - set(lis1)) print("Unique values from two lists:",z,m)

        Here is the Screenshot of the following given code

        Get unique values from two list Python

        As you can see in the screenshot the output displays the unique values from two lists

        Get unique value from list Python using index

        • To get the unique value from a list with an index number, we can easily use the concept of Python list.index() method.
        • To perform this particular task first, we will create a list and assign integer values to it. After that, we are going to use the list comprehension method and it will iterate the list values.
        new_list = [2,6,9,2,9,4] final_output = [new_list.index(i) for i in set(new_list)] print("Unique values get index from list:",final_output)

        Here is the execution of the following given code

        Get unique value from list Python using index

        As you can see in the Screenshot the output displays the index number of unique values

        Get unique values from list of lists Python

        • In this section, we will discuss how to get the unique values from list of lists by using Python.
        • By using the list comprehension method we can solve this problem. In this example we have to iterate over values by listing objects and getting the unique values. In Python, list comprehension is the easiest way to declare a new list from the given list Python.

        Let’s take an example and check how to get the unique values from a list of lists in Python

        list_values = [[74,35,74], [189,74,55], [35,74,128]] new_output = print("Unique values from list of lists:",new_output)

        In the above code, we created a list ‘list_values’ and then iterate the ‘i’ variable over the ‘list_values’. Once you will print ‘new_output’ then the result will display the unique values from a list of lists.

        You can refer to the below Screenshot

        Get unique values from list of lists Python

        Get unique values from list of strings Python

        • In this Program, we will discuss how to get the unique values from a list of strings by using Python.
        • To perform this particular task, we can easily use the ordered.dict() method from the collection module by using Python. After that, we will create a list and assign string values to it.
        • Now declare a variable ‘Final_output’ and within this variable use the list function and pass OrderedDict.fromkeys() method. It will help the user to get the unique values from the Python list.
        from collections import OrderedDict list_values = ['John', 'Micheal', 'Smith', 'John', 'George', 'Micheal','William' , 'Smith'] Final_output= list(OrderedDict.fromkeys(list_values)) print("Unique values from list of strings:",Final_output)

        Here is the implementation of the following given code

        Get unique values from list of strings Python

        In this Python tutorial, we learned how to get unique values from lists by using Python. Also, we will cover these topics.

        1. Get unique values from list Python Pandas
        2. Get unique values from list Python
        3. Get unique values from nested list Python
        4. Get unique values from list of dictionary Python
        5. Python get unique values from list of objects
        6. Remove unique values from list Python
        7. Get unique values from two list Python
        8. Get unique value from list Python using index
        9. Get unique values from list of lists Python
        10. Get unique values from list of strings Python

        I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.

        Источник

        Читайте также:  Grey color for html
Оцените статью