We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Python
  3. Sets
  4. Set .discard(), .remove() & .pop()
  5. Discussions

Set .discard(), .remove() & .pop()

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 576 Discussions, By:

votes

Please Login in order to post a comment

  • DeadMoroz
    6 years ago+ 69 comments
    n = int(input())
    s = set(map(int, input().split())) 
    for i in range(int(input())):
        eval('s.{0}({1})'.format(*input().split()+['']))
    
    print(sum(s))
    
    287|
    Permalink
    View more Comments..
  • vikasgothwal0
    6 years ago+ 19 comments

    Easy code for beginners Python3

    n=int(input())

    s = set(map(int,input().split()))

    N=int(input())

    for i in range(N) :

    choice=input().split()
    if choice[0]=="pop" :
        s.pop()
    elif choice[0]=="remove" :
        s.remove(int(choice[1]))
    elif choice[0]=="discard" :
        s.discard(int(choice[1]))
    

    print (sum(s))

    103|
    Permalink
    View more Comments..
  • bhatiaankur8
    7 years ago+ 3 comments

    Since the pop operation removes an arbitary element from the set, how do we get the same answer?

    36|
    Permalink
  • rschmidt_z
    6 years ago+ 7 comments
    n = int(input())
    s = set(map(int, input().split())) 
    t = int(input())
    
    for i in range(t):
    
        c, *args = map(str,input().split())
    
        getattr(s,c) (*(int(x) for x in args))
    
    
    print (sum(s))
    
    33|
    Permalink
    View more Comments..
  • emailmihirp
    3 years ago+ 2 comments

    POP is not guaranteed to work as expected, it doesnt POP same element on all python versions. my solution was failing on "pypy2" becasue always last elemnt in set was popped. But same code passed when selected python2.

    24|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature