Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert multiple elements into a list?

In JavaScript, I can use splice to insert an array of multiple elements in to an array: myArray.splice(insertIndex, removeNElements, ...insertThese).

But I can't seem to find a way to do something similar in Python without having concat lists. Is there such a way? (There is already a Q&A about inserting single items, rather than multiple.)

For example myList = [1, 2, 3] and I want to insert otherList = [4, 5, 6] by calling myList.someMethod(1, otherList) to get [1, 4, 5, 6, 2, 3]

like image 946
TheRealFakeNews Avatar asked Sep 16 '16 23:09

TheRealFakeNews


People also ask

How do I add multiple items to a list?

You can use the sequence method list. extend to extend the list by multiple values from any kind of iterable, being it another list or any other thing that provides a sequence of values. So you can use list. append() to append a single value, and list.

Which function is used to add multiple elements to a list?

When you wish to append multiple items to a list and the goal is to store them as separate entities/elements, use the extend function. Since the extend function has been implemented in C, it would be comparatively quicker than append .

How do you add elements to an n list in Python?

append(input()) print("\nEnter an Element to Insert at End: ") elem = input() nums. append(elem) numsSize = numsSize+1 print("\nThe New List is: ") for i in range(numsSize): print(end=nums[i] + " ") print() except ValueError: print("\nInvalid Input!


3 Answers

To extend a list, you just use list.extend. To insert elements from any iterable at an index, you can use slice assignment...

>>> a = list(range(10))
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[5:5] = range(10, 13)
>>> a
[0, 1, 2, 3, 4, 10, 11, 12, 5, 6, 7, 8, 9]
like image 53
mgilson Avatar answered Oct 05 '22 22:10

mgilson


Python lists do not have such a method. Here is helper function that takes two lists and places the second list into the first list at the specified position:

def insert_position(position, list1, list2):
    return list1[:position] + list2 + list1[position:]
like image 30
RFV Avatar answered Oct 05 '22 23:10

RFV


I'm not certain this question is still being followed but I recently wrote a short code that resembles what is being asked here. I was writing an interactive script to perform some analyses so I had a series of inputs serving to read in certain columns from a CSV:

X = input('X COLUMN NAME?:\n')
Y = input('Y COLUMN NAME?:\n')
Z = input('Z COLUMN NAME?:\n')
cols = [X,Y,Z]

Then, I brought the for-loop into 1 line to read into the desired index position:

[cols.insert(len(cols),x) for x in input('ENTER COLUMN NAMES (COMMA SEPARATED):\n').split(', ')]

This may not necessarily be as concise as it could be (I would love to know what might work even better!) but this might clean some of the code up.

like image 45
William Taylor Avatar answered Oct 05 '22 23:10

William Taylor