MATLAB Examples

Index into Python dict

This example shows how to get and set values in a dictionary.

Create a dictionary variable.

customers = py.dict
customers = 

  Python dict with no properties.

    {}

Populate the dict variable with customer names and account numbers and display the results. The output depends on your Python® version.

customers{'Smith'} = int32(2112);
customers{'Anderson'} = int32(3010);
customers{'Audrey'} = int32(4444);
customers{'Megan'} = int32(5000);
customers
customers = 

  Python dict with no properties.

    {'Smith': 2112, 'Megan': 5000, 'Audrey': 4444, 'Anderson': 3010}

Read the account number for customer Anderson.

acct = customers{'Anderson'}
acct = 

  Python int with properties:

    denominator: [1×1 py.int]
           imag: [1×1 py.int]
      numerator: [1×1 py.int]
           real: [1×1 py.int]

    3010