convert query in python

Vineet S 1,390 Reputation points
2024-10-26T06:52:14.69+00:00

HI

how to convert this query in python

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

get the unique values from the given list without using built in function

 B = [1,2,3,4,5] 
Azure Databricks
Azure Databricks
An Apache Spark-based analytics platform optimized for Azure.
2,514 questions
0 comments No comments
{count} votes

Accepted answer
  1. phemanth 15,755 Reputation points Microsoft External Staff Moderator
    2024-10-28T08:48:11.03+00:00

    @Vineet S

    Thanks for reaching out on Microsoft Q&A.

    To extract unique values from a list in Python without using built-in functions, you can use a simple loop to check for duplicates. Here’s how you can do it:

    User's image

    A = [1, 2, 1, 1, 1, 2, 2, 3, 4, 5, 5, 5, 5]
    B = []
    for item in A:
        if item not in B:
            B.append(item)
    print(B)  # Output: [1, 2, 3, 4, 5]
    

    Explanation:

    1. Initialize an empty list B to store unique values.
    2. Loop through each item in list A.
    3. Check if the item is not already in B. If it’s not, append it to B.
    4. Finally, print B to see the unique values.

    This method effectively builds a new list of unique items without using any built-in functions

    Hope this helps. Do let us know if you any further queries.


    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Gowtham CP 6,020 Reputation points Volunteer Moderator
    2024-10-26T07:01:57.9166667+00:00

    Hello Vineet S ,

    Thanks for reaching out on Microsoft Q&A.

    First create a new list to store these unique values. Then iterate through the original list, checking if each element is already present in the new list. If not, add it to the new list.

    A = [1, 2, 1, 1, 1, 2, 2, 3, 4, 5, 5, 5, 5]
    B = []  # Initialize an empty list to store unique values
    
    for item in A:
        # Add item to B if it's not already present
        if item not in B:
            B.append(item)
    
    print(B)  # Output will be [1, 2, 3, 4, 5]
    
    

    Hope this helps!

    If you found this answer helpful, please upvote and mark it as accepted to close the thread. Thanks!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.