Error: Function did not correctly modify the value of argument number 1

I’ve been getting this error for several problems: Function did not correctly modify the value of argument number 1.
Could you please help me understand.

https://app.dataquest.io/m/1018/lists/17/extending-lists

def insert(list1,obj,idx):
    return list1[:idx]+[obj]+list1[idx:]

Function insert did not correctly modify the value of argument number 1.

1 Like

Hey @piya ,

We built our Practice Mode in an effort to help you sharpen or brush up on specific skills, and to help you know where you are in your learning journey. Practice Mode may seem a bit more difficult, but we encourage you to keep working at it as it’ll help you grow and learn new things.

We want to encourage you to figure out you own unique solutions to these problems. :slight_smile:

Ok :smiley:
But I did try! Getting blocked on several problems because, clearly, I’m making a common error across all the above problems. A clue perhaps? Please :smiley:

Hey @piya,

I’m gonna try to help you without doing any “spoon-feeding”. To do that, I’m gonna attack what I think is the underlying problem instead directly answering the questions that you are asking (which I know the solution to :wink: ).

It seems to me that you need to get more clarity in how lists and functions work in Python. You use the basic syntax correctly but these practice problems kindof go into the depths of how they work. I would advise you to get a more thorough understanding of these concepts and try to debug your solutions.

3 Likes

Nityesh, Thank you for encouraging me to poke my brain some more :slight_smile:

My solution works when I run it. I only get the error message when I ‘Submit Answer’.
The solution igiven involves using ‘extend’. I’m just trying to solve without using the function.

Does this has anything to do with the DQ IDE?
I tried this code on repl.it and it is working there as well.

For your reference my solution is: def extend_list(x,y):
x = x+y
return x
print(x)

extend_list(1,2)

Thanks.

Hey, Piya.

Solution to what? You linked to three different problems in this topic. Please ask more focused questions.

What do you mean it works when you run it? It working for a specific input doesn’t mean it works in general.

Here’s a hint: do you know what “in place” (a term used in some of the instructions) means?

1 Like

Bruno, I am referring to this problem.
https://app.dataquest.io/m/1018/lists/17/extending-lists

In the DQ IDE, you have ‘Run Code’ and ‘Submit Answer’
Solution works when I ‘Run Code’ but throws up error on ‘Submit Answer’

“modify the first argument in place” - I assume modify list1? x in this case
def extend_list(x,y):
x = x+y
return x
print(x)

extend_list([1,2],[3,4])

The solution I typed results in the same output as the solution given my DQ.
#DQ Solution
def extend_list(some_list, another_list):
some_list.extend(another_list)
return(some_list)

#P’s solution
def extend_list(x,y):
x = x+y
return x

Run both of these with:
extend_list([1,2],[3,4])
You get the same answer.

It looks like a problem at your end.

Running code only runs the code, only when you submit is the code evaluated. You’re getting feedback that your code didn’t work because it is not a solution.

Your understanding of this is flawed, hence the confusion. I suggest you research what modifying in place means.

1 Like

Please see this guide on how to write markdown so that you can properly format your posts.

Your solution is incorrect. Let’s see this.

>>> def extend_list(x,y):
...     x = x+y
...     return x
... 
>>> x = [1,2]
>>> y = [3,4]
>>> extend_list(x,y)
[1, 2, 3, 4]

The output is indeed correct. It’s important to understand, however, what is meant with “output” here. It roughly means “stuff that is printed to the screen”. Nevertheless…

>>> print(x)
[1, 2]

The input list is still the same! Your solution didn’t modify it. I reiterate the tip I’ve been giving you: research what it means to modify in place.

1 Like

I’m going to look into this some more.

For the record I got the same output with print(x). I don’t know why you get [1,2]

I do think however that giving me the answer is appropriate in this situation. This is not spoon feeding. I’m not asking a lazy question. I’ve spent hours trying to figure this out and still don’t get it.

I’m quite disappointed.

Maybe you had the actual solution in memory instead of your solution. It’s simply not possible for you to get the same output with your solution.

Struggling is good :muscle: It makes for a great learning experience, even if it makes us feel miserable (there’s research to back this up).

You’re struggling because you’re missing a fundamental concept. It’s not that you can’t solve it, it’s that you’re trying to go to the bottom of a pool without getting wet without having the right tools for this job.

You already have the solution. You pasted it above. Can you please clarify what you want?

I don’t have the solution, because ‘submit answer’ continues to throw an error.

I’m not going to get agitated over this problem anymore.
Let’s close this thread.

You can find the solution by clicking the arrow next to Get Help and then clicking on See the answer.

If this doesn’t work for you, then there is a problem on our end.

Of course, I have seen the solution proposed by DQ. I looked at it yesterday/day before when I first tried this proble.
I just wanted to know why the code I wrote wouldn’t work.

3 Likes

Edit to add:
For every question on this post and the other posts, I have seen the DQ Solution. It’s easy enough to locate.

I don’t want to use the ready-made Python methods being used in the solutions. The way I strengthen my knowledge is to work from scratch to begin with. I am trying to solve using multiple approaches.

And this is why I say, this is not “spoon feeding”. I don’t think I’m “diving into a pool without getting wet” either - I have completed the first course until the chapter on Installing Jupyter. I’ve been reluctant to proceed further without completing the Practice Problems.

“modify in place” - my understanding is, it refers to global and local scope. I will need to dig in.

3 Likes

Argh. I got it. Finally. Duh. With the help of a good samaritan.
I used id function to resolve this.

So there is only two ways to do this.
a. use extend function
b.list1+=list2

PS: No longer mentally constipated :smiley:

2 Likes

That’s so good to hear @piya! I hope this has been a helpful exercise for you. Also, thanks a lot for your feedback!! :slight_smile:

1 Like

Same here. Did you figure out how to write the function without using python’s base function? I tried the following but it still doesn’t work : /

def extend_list(some_list, another_list):
    some_list = some_list[:] + another_list[:]
      
quote = ['Aus', 'dem', 'Paradies,', 'das', 'Cantor', 'uns', 'geschaffen,']
rest_of_quote = ['soll', 'uns', 'niemand', 'vertreiben', 'können.']
extend_list(quote, rest_of_quote)
print(quote)

Output: [‘Aus’, ‘dem’, ‘Paradies,’, ‘das’, ‘Cantor’, ‘uns’, ‘geschaffen,’]

quote = quote[:] + rest_of_quote[:]
print(quote)

Output: [‘Aus’, ‘dem’, ‘Paradies,’, ‘das’, ‘Cantor’, ‘uns’, ‘geschaffen,’, ‘soll’, ‘uns’, ‘niemand’, ‘vertreiben’, ‘können.’]