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

  • Home
  • SEARCH
  • 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 8694819
In Process

The Archive Base Latest Questions

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

Possible Duplicate: python takes list and returns only if negative value also exists using

  • 0

Possible Duplicate:
python takes list and returns only if negative value also exists using set

I’m having problems with another homework problem.

By using a set, write a method negated(a) that takes a list, a, as an
argument and returns a list containing only the elements x such that
-x is also in a

His example shows that the input is

[-6, 8, 7, 3, 2, -9, 1, -3, 2, -4, 4, -8, 7, 8, 2, -2, -7, 0, 1, -9,
-3, -7, -3, -5, 6, -3, 6, -3, -10, -8]

and the output is

[-6, 8, 7, 3, 2, -3, 2, -4, 4, -8, 7, 8, 2, -2, -7, 0, -3, -7, -3, 6,
-3, 6, -3, -8]

I was able to figure out how to do it without using a set with

return [x for x in a if -x in a]

I’m just having problems implementing a set into the problem.
Can someone give me the steps to take, how I should tackle the problem… I’m not looking for the complete work, but it would be nice to see how you do it 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:52:36+00:00Added an answer on June 13, 2026 at 12:52 am

    Here’s the algorithm that you need to implement:

    1. iterate through all elements in the list
    2. when you consider each individual element, check if its negation exists in the list
    3. if the negation exists in the list, keep it
    4. if the negation does not exist in the list, don’t keep it.

    It’s usually a bad idea to delete elements from a list as you’re iterating over it, so it’s best to make a new empty list and keep adding (or not) to it.

    def filterNegs(L):
        answer = []
        for i in L:
            if -1*i in L:
                answer.append(i)
        return answer
    

    Here’s a list comprehension for the same:

    return [i for i in a if -1*i in a]
    

    There’s a performance issue when you do this though. Checking if an element is in a list is an O(n) operation and O(1) in a set. So, it’s better to turn L into a set first:

    def filterNegs(L):
        L = set(L)
        answer = []
        for i in L:
            if -1*i in L:
                answer.append(i)
        return answer
    

    Here’s a list comprehension for the same:

    L = set(L)
    return [i for i in a if -1*i in a]
    

    Hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Python : how to append new elements in a list of list?
Possible Duplicate: Python: What is the best way to check if a list is
Possible Duplicate: Python, compute list difference I have two lists For example: A =
Possible Duplicate: Python code to pick out all possible combinations from a list? I
Possible Duplicate: Finding first and last index of some value in a list in
Possible Duplicate: Command Line Arguments In Python I am using Python 3.2. What I
Possible Duplicate: Python: simple list merging based on intersections I have a multiple list:
Possible Duplicate: Python List vs. Array - when to use? I'm working on a
Possible Duplicate: Python “extend” for a dictionary I know that Python list can be
Possible Duplicate: Python list problem I try to initialise a matrix in python. First

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.