How to Fix Typeerror: Insert Expected 2 Arguments, Got 1 in Python Flask?

TypeError: insert expected 2 arguments, got 1

so, when i got objects in fuction get, and then i want that value in my dataUser,

ive old list (output) [{ 'username':'budi'},{ 'username':'jhon'}] and i wanna old list to new list (dataUser)

dataUser = []
print(dataUser)
class users(Resource):
# Get data
def get(self):
    query = User.query.all()
    output = [
        {
            "username":data.username
        }
        for data in query
    ]
    response = {
        "code" : 200, 
        "msg"  : "Query data sukses",
        "data" : output
    }
    for item in output:
        print(item)
        dataUser.insert(item)
    return response['msg']

so i can set up those value in Post

    def post(self):
    UsersUsername = request.form['username']
    UsersPassword = request.form['password']
    UsersConfPassword = request.form['confPassword']
    try:
        if(UsersPassword == UsersConfPassword | datauser.username != UsersUsername) : // <-- to this
            model = User(username=UsersUsername, password=UsersPassword)
            model.save()
            response = {
                "msg" : "Data berhasil dimasukan",
                "code": 200 
            }
            return response['msg']
    except:
            response = {
                "msg" : "Data Tidak Berhasil dimasukan dikarenakan username / password salah",
                "code": 400 
            }
            return response["msg"]
4

3 Answers

insert indeed takes two arguments - the index to insert to and the object to insert. If you just meant to add all the items to the dataUser list, you could use append instead:

dataUser.append(item)
1

If you want to add a new value to a list, use append to add a new item to the end of the list

dataUser = []
dataUser.append("first item")

insert adds the value at the given positions,

...
dataUsers.insert(1, "second item")
# roughly equivalent to
dataUsers[1] = "second item"
0

The issue here that you use insert(index, value) method that need two arguments follow doc: ex from w3school

fruits = ['apple', 'banana', 'cherry']
fruits.insert(1, "orange") 

please fix your code with this

dataUser.append(item)

it will work.

1

Your Answer

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

Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest