You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已创建嵌套字典后,如何访问其中的元素?

Accessing Elements in Your Nested Dictionary

Let's walk through exactly how to pull values from your nested dictionary using concrete examples based on the main_dict you shared. First, here's your dictionary for quick reference:

main_dict = {
    'pins': {
        'Category': ['General'],
        'Contact': ['Mark'],
        'Description': ['This', 'is', 'a']
    },
    'nails': {
        'Category': ['specific'],
        'Contact': ['Jon'],
        'Description': ['This', 'is', 'a', 'description']
    },
    'board': {
        'Category': ['General'],
        'Contact': ['Mark'],
        'Description': ['This', 'is', 'a']
    },
    'hammer': {
        'Category': ['tools'],
        'Contact': ['Jon'],
        'Description': ['This', 'is', 'a', 'description']
    }
}

1. Grab top-level inner dictionaries

Start by accessing the main keys (like 'pins' or 'nails') to get the entire nested dictionary for that item:

# Get all data for 'pins'
pins_info = main_dict['pins']
# Result: {'Category': ['General'], 'Contact': ['Mark'], 'Description': ['This', 'is', 'a']}

2. Access specific fields in inner dictionaries

Chain square brackets to dig directly into nested fields. You can do this in one line or break it into steps:

  • Get the Category value for 'nails':
nails_category = main_dict['nails']['Category']
# Result: ['specific']
  • Get the Contact value for 'hammer':
hammer_contact = main_dict['hammer']['Contact']
# Result: ['Jon']

3. Pull individual items from the list values

Most of your nested values are lists. Add an index number to grab specific elements from those lists:

  • Get the first word in the 'pins' Description:
pins_first_desc_word = main_dict['pins']['Description'][0]
# Result: 'This'
  • Get the last word in the 'nails' Description (use -1 to target the last list item):
nails_last_desc_word = main_dict['nails']['Description'][-1]
# Result: 'description'

4. Safe access to avoid key errors

If you're not sure a key exists (e.g., checking for a missing item like 'screws'), use the get() method instead of square brackets. It returns None (or a custom default) instead of throwing an error:

# Safe get for a non-existent top-level key
screws_data = main_dict.get('screws', 'No data available for this item')
# Result: 'No data available for this item'

# Chain get() for nested keys
board_contact = main_dict.get('board', {}).get('Contact', 'Unknown contact')
# Result: ['Mark']

5. Loop through all entries

If you want to iterate over every item in the dictionary, use items() to loop through key-value pairs:

  • Print all item names and their primary contact:
for item_name, item_details in main_dict.items():
    print(f"Item: {item_name}, Contact: {item_details['Contact'][0]}")

Output:

Item: pins, Contact: Mark
Item: nails, Contact: Jon
Item: board, Contact: Mark
Item: hammer, Contact: Jon

内容的提问来源于stack exchange,提问作者Heyya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:34:38