logoalt Hacker News

wwfntoday at 5:11 PM4 repliesview on HN

Tangential (but topical in that "The threat is comfortable drift toward not understanding what you're doing" is also on the front page):

Is the generated python code in the example wrong?

The prompt

> Develop a Python function that removes any falsey values from a list. Return the modified list without creating a new one.

Is answered with list comprehension, which makes a new list and leaves the original unmodified (never mind that the *args input necessarily can't be a modifiable list?)

   def remove_falsey_values(*args): return [val for val in args if val]
Whereas I'd expect something like

    def remove_falsey_values(l):
          for i in reversed(range(len(l))):
               if not l[i]: l.pop(i)
          # returned list is linked to input l 
          return l

    a = [1, 0, False, 'foo']
    x = remove_falsey_values(a)
    x[0] = 2
    print(a) # [2,'foo']

Replies

hecanjogtoday at 5:22 PM

It doesn't fit the requirement to modify the list in place, but the prompt itself contradicts the requirements by asking explicitly for the implementation to use *args and a list comprehension.

show 1 reply
desideratumtoday at 8:17 PM

Oh I wouldn't be surprised. This is a sample from one of the OSS code datasets I'd used, which are all generated synthetically using LLMs. Data is indeed the moat.

semiinfinitelytoday at 8:22 PM

your second function is the type of bad code you get from people trying to program python like its c

__stoday at 7:04 PM

    def remove_falsey_values(l):
          l[:] = (x for x in l if x)