insert

How to insert multiple elements into a list?

How to insert multiple elements into a list? Question: 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 …

Total answers: 7

Prepend element to numpy array

Prepend element to numpy array Question: I have the following numpy array import numpy as np X = np.array([[5.], [4.], [3.], [2.], [1.]]) I want to insert [6.] at the beginning. I’ve tried: X = X.insert(X, 0) how do I insert into X? Asked By: piRSquared || Source Answers: numpy has an insert function that’s …

Total answers: 5

Insert element in Python list after every nth element

Insert element in Python list after every nth element Question: Say I have a Python list like this: letters = [‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’,’i’,’j’] I want to insert an ‘x’ after every nth element, let’s say three characters in that list. The result should be: letters = [‘a’,’b’,’c’,’x’,’d’,’e’,’f’,’x’,’g’,’h’,’i’,’x’,’j’] I understand that I can do that with looping and …

Total answers: 11

How to add element in Python to the end of list using list.insert?

How to add element in Python to the end of list using list.insert? Question: There is a list, for example, a=[1,2,3,4] I can use a.append(some_value) to add element at the end of list, and a.insert(exact_position, some_value) to insert element on any other position in list but not at the end as a.insert(-1, 5) will return …

Total answers: 3

Python pandas insert list into a cell

Python pandas insert list into a cell Question: I have a list ‘abc’ and a dataframe ‘df’: abc = [‘foo’, ‘bar’] df = A B 0 12 NaN 1 23 NaN I want to insert the list into cell 1B, so I want this result: A B 0 12 NaN 1 23 [‘foo’, ‘bar’] Ho …

Total answers: 9

Postgres: Is there a way of executing code following a INSERT statement?

Postgres: Is there a way of executing code following a INSERT statement? Question: This may seem strange, but I was curious to know if it was possible for a code block to be executed following an INSERT statement in a postgres database? Specifically, I’m interested in executing Python code after an INSERT statement has occurred …

Total answers: 1

Django: Insert row into database

Django: Insert row into database Question: I’m new in Django. I’ve created a table by insert model into models.py. Now, I want to insert a row into the database – table Dodavatel. I know, that I have to create an object with attributes as columns. But I don’t know where should I put this code. …

Total answers: 2

Insert at first position of a list in Python

Insert at first position of a list in Python Question: How can I insert an element at the first index of a list? If I use list.insert(0, elem), does elem modify the content of the first index? Or do I have to create a new list with the first elem and then copy the old …

Total answers: 2

TypeError: 'float' object is not subscriptable

TypeError: 'float' object is not subscriptable Question: PizzaChange=float(input(“What would you like the new price for all standard pizzas to be? “)) PriceList[0][1][2][3][4][5][6]=[PizzaChange] PriceList[7][8][9][10][11]=[PizzaChange+3] Basically I have an input that a user will put a number values (float input) into, then it will set all of these aforementioned list indexes to that value. For some reason …

Total answers: 5

Python psycopg2 not inserting into postgresql table

Python psycopg2 not inserting into postgresql table Question: I’m using the following to try and insert a record into a postgresql database table, but it’s not working. I don’t get any errors, but there are no records in the table. Do I need a commit or something? I’m using the postgresql database that was installed …

Total answers: 4