5
\$\begingroup\$

I am a newbie trying to insert elements after the n:th element in the list. Any shorter/smarter ways?

def InsertElements(listName, var1, var2, n):
 listName.insert(n, var1)
 listName.insert(n+1, var2)
 return listName
myList = ["banana", "orange", "apple", "cherry", "grape"]
result = InsertElements(myList, "mango", "papaya", 3)
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Feb 25, 2015 at 20:28
\$\endgroup\$
3
  • \$\begingroup\$ doesn't have .add(element) method ? \$\endgroup\$ Commented Feb 25, 2015 at 20:31
  • \$\begingroup\$ If you have a list of things to insert, you could do newlist = list[:n] + added_elements + list[n:]. \$\endgroup\$ Commented Feb 25, 2015 at 20:32
  • \$\begingroup\$ For some reason there is a "one line frenzy" going on in SO. I highly doubt that short code means good code. Anyway.. your code seems fine, excluding the return part which is somewhat redundant. insert() modifies your list so you can simply call your list after running your function without the need of a return inside it. \$\endgroup\$ Commented Feb 25, 2015 at 20:56

2 Answers 2

11
\$\begingroup\$

Many languages (e.g. JavaScript, Perl) have a splice function of some sort to modify list contents. They allow you to insert list items or change existing ones to new sublists.

Python uses a "slice" approach, allowing you to specify many list edits without a specific splice function.

>>> myList = ["banana", "orange", "apple", "cherry", "grape"]
>>> myList[3:3] = ["mango", "papaya"]
>>> myList
['banana', 'orange', 'apple', 'mango', 'papaya', 'cherry', 'grape']

By the way, if this is hard to understand, this cheat sheet may help:

 ["banana", "orange", "apple", "cherry", "grape"]
 0_______ 1_______ 2______ 3_______ 4______ 5 # item index
 0:1_____ 1:2_____ 2:3____ 3:4_____ 4:5____ # equiv slice notation
 ^ ^ ^ ^ ^ ^ # slice notation for
 0:0 1:1 2:2 3:3 4:4 5:5 # 0_length positions
 # between items
 0:2________________ 2:5_______________________ # misc longer slice 
 1:4_________________________ # examples
 1:5_________________________________
 1:__________________________________
 0:____________________________________________
 :_____________________________________________
 :3__________________________
 :1______

There are also negative indices (counting from the end of the list). Let's not get into those right now!

answered Feb 25, 2015 at 20:32
\$\endgroup\$
1
  • \$\begingroup\$ One thing I wish I'd explained a little better: When you ask a list for a single value (e.g. mylist[3]) you get back a single item. If you use the slice notation, you get back a list. Slices are also indexing, but they do not reduce the dimensionality of the result like integer indices do. \$\endgroup\$ Commented Nov 30, 2017 at 15:45
3
\$\begingroup\$

Note that .insert will modify the list in-place, so after the call result and myList will refer to the same list.

Your code is OK. If you wanted a bit of a shorter option, you could use slicing:

listName[n:n] = [var1, var2]

This inserts both var1 and var2 after the nth item in the list.

answered Feb 25, 2015 at 20:34
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.