Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8694955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:54:44+00:00 2026-06-13T00:54:44+00:00

I have a little code that takes a list of objects, and only outputs

  • 0

I have a little code that takes a list of objects, and only outputs the items in the list that are unique.

This is my code

def only_once(a):
    return [x for x in a if a.count(x) is 1]

My teacher requires us to use sets for this function though.
Can someone show me what I can do?

My code has to take an input such as a=[1,4,6,7,3,2,4,5,7,5,6], and output [1, 3, 2]. Has to retain it’s order also.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:54:45+00:00Added an answer on June 13, 2026 at 12:54 am

    [I’m assuming that you’re also user1744238 and user1744316 — please pick a username and stick to it, that way it’s easier to check to see what variants of a question you’ve asked and what you’ve already tried.]

    One set-based approach is to use two sets as a counter. You only care about whether you’ve seen something once or more than once. For example, here’s an easy-to-explain approach:

    1. Make an empty set for once and more.
    2. Loop over every element of your list, and:
      1. If you haven’t seen it before, add it to once.
      2. If you’ve seen it once, remove it from once and add it to more.
    3. Now you know what elements you’ve seen exactly once, in the set once.
    4. Loop over the elements of the list, and if you’ve seen it once, add it to the output list, and remove it from the once set so you don’t output the same element twice.

    This gives me:

    In [49]: f([1,4,6,7,3,2,4,5,7,5,6])
    Out[49]: [1, 3, 2]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little tool written in JavaScript and PHP that takes a list
so im a little confused here.... i have code that takes info from my
I have a simple little code fragment that is frustrating me: HashSet<long> groupUIDs =
I have a little bit of Javascript that almost works correctly. Here's the code:
I have a little problem. This is my code: stackPanelShapesContainer = new StackPanel(); InitializeStackPanle();
I'm writing a filter that will pick out items. I have a list of
This may be a little subjective, but I have often found that it can
I have the following code that takes an SQL statement (string), loads the results
I was writing a little program that takes a list and generates a menu
I have a little private Google code project. For a SCM I use Mercurial.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.