HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonModerate

Insert elements into a list

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
intolistinsertelements

Problem

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)

Solution

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!

Code Snippets

>>> myList = ["banana", "orange", "apple", "cherry", "grape"]
>>> myList[3:3] = ["mango", "papaya"]
>>> myList
['banana', 'orange', 'apple', 'mango', 'papaya', 'cherry', 'grape']
["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______

Context

StackExchange Code Review Q#83338, answer score: 11

Revisions (0)

No revisions yet.