[Python] How to insert at the first index in a list of dictionary [closed]

Discussão em 'Python' iniciado por Stack, Abril 20, 2025.

  1. Stack

    Stack Membro Participativo

    I have a dictionary as follows:

    user = [
    {
    'Customer Name' : 'Jane'
    ,'Greet' : 'Welcome to Lidl'
    ,'basket ': ['Onions','Grapes','Bananas','Cheese','Cucumbers']
    ,'Customer age ': 35
    },
    {
    'Customer Name': 'Irene'
    ,'Greet' : 'Welcome to Lidl'
    ,'basket ': ['Onions','Grapes','Bananas','Cheese','Cucumbers','Coffee']
    ,'Customer age ': 50
    }
    ]


    Would like to add more to the basket at index 1 and append the list with another key: value pair.

    Expected Result :

    user = [
    {
    'Customer Name' : 'Jane'
    ,'Greet' : 'Welcome to Lidl'
    ,'basket ': ['Onions','Grapes','Bananas','Cheese','Cucumbers']
    ,'Customer age ': 35
    },
    {
    'Customer Name': 'Irene'
    ,'Greet' : 'Welcome to Lidl'
    ,'basket ': ['Onions','Grapes','Bananas','Cheese','Cucumbers','Coffee','Kale']
    ,'Customer age ': 30
    }
    ]


    So added Kale to the basket and changed customer age to 30.

    Continue reading...

Compartilhe esta Página