2

I'm having trouble with this question:

Write a function insert_item_end(data, item) that takes a list data, and item as a parameter and returns a new list that contains item at the end of the data using the insert method (i.e. you cannot use append).

This is what I've done so far:

data.insert(-1, item)

However, this only returns None.

Here is an example of what I want to return:

([1, 2, 3, 4, 5], 1) returns [1, 2, 3, 4, 5, 1]

4
  • list.insert always returns None. But it modifies the list object in-place. Note also, it inserts before the index, so data.insert(-1, 1) would give you [1,2,3,4,1,5] Commented Oct 7, 2020 at 12:04
  • So, your instructions say you need to create a new list. You could use .insert (although, inserting at the end is just .append) to do this, but you would have to copy the incoming list first. Then modify that list, and return it. There is probably a cleaner way to do that than explicitly copying... Commented Oct 7, 2020 at 12:06
  • To be painfully honest; this is a ridiculous exercise as it 1) uses insert in a way in which it was not designed (i.e. the reason we have append) and 2) forces the student to write verbose code, which is neither efficient nor Pythonic. Suggestion: Find a new course. Quick! Commented Oct 7, 2020 at 12:26
  • ...or; perhaps stick with the course and when you see a problem that seems counter intuitive, challenge it. As you as doing here. There is usually a very simple solution when it comes to Python. Commented Oct 7, 2020 at 12:30

3 Answers 3

1

Do this suit your requirement?

def insert_item_end(data,item):
    newlist=[];
    newlist=data
    newlist.insert(len(data),item)
    print(newlist)


data=[1, 2, 3, 4, 5];
item=1

insert_item_end(data, item)
Sign up to request clarification or add additional context in comments.

1 Comment

You can eliminate the first line of the function, as you're anyways redefining newlist on the second.
0

you can use

data.insert(len(data),item)

technically -1 and len(data)-1 should also work but i don't know why its inserting at the second last position.

2 Comments

@Aryman_Deshwal - The reason data.insert(len(data) - 1, item) inserts the item into the second to last position is because the length of a list, as returned by len is always going to be 1 greater than the last index of the list. So if there are three items in the list, and you want to add an item to the end of the list, you'll want to add an item to data[3], because a list's index starts being counted at 0, so right now there are elements at data[0], data[1], and data[2]. The fourth element you add will be data[3].
Now, len(data) returns 3, which is one greater than the last index of the list: 2. So if you subtract one from this, you'd be adding item to data[2], and whatever was already at data[2] gets pushed to data[3]. That's why item ends up in the second to last position.
0

You can simplify @Priyankarao's function by simply modifying the list that's passed to your function, instead of defining a new one. Something like this:

def append_to_list(data,item):
    data.insert(len(data),item)
    return data

Then run it like so:

nums = [1, 2, 3, 4, 5];
item = 6

newlist = append_to_list(nums, item)
print(newlist)

Output:

[1, 2, 3, 4, 5, 6]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.