Python嵌套字典遍历

Python嵌套字典遍历,python,list,dictionary,Python,List,Dictionary,我是python新手,在找到一种完成任务的好方法时遇到了一些困难 我有一本类似的字典 x = { "rackList": [ { "rackType": "apc", "serverList": [ { "serverType": "x4950", "serverIp": "192.168.0.1", } ], "

我是python新手,在找到一种完成任务的好方法时遇到了一些困难

我有一本类似的字典

x = {
"rackList": [
    {
        "rackType": "apc",
        "serverList": [
            {
                "serverType": "x4950",
                "serverIp": "192.168.0.1",
            }
        ],
        "position": 1
    },
    {
        "rackType": "apc",
        "serverList": [
            {
                "serverType": "x4950",
                "serverIp": "192.168.0.2",
            }
        ],
        "position": 2
    },
    {
        "rackType": "apc",
        "serverList": [
            {
                "serverType": "x4950",
                "serverIp": "192.168.0.3",
            }
        ],
        "position": 3
    },
    {
        "rackType": "apc",
        "serverList": [
            {
                "serverType": "x4950",
                "serverIp": "192.168.0.4",
            }
        ],
        "position": 4
    }
]}
我需要从每个服务器列表中提取服务器IP,因此目前我正在执行以下操作:

y = []

for i in x['rackList']:
    for j in i['serverList']:
        y.append(j['serverIp'])
我想知道是否有一种更像python的或者更简单的方法可以达到同样的效果

提前感谢

是的,它被称为:

是的,它叫a:


非常感谢您的快速回复,这就是我要找的!非常感谢您的快速回复,这就是我要找的!
y = [server['serverIp'] for rack in x['rackList'] for server in rack['serverList']]
# ['192.168.0.1', '192.168.0.2', '192.168.0.3', '192.168.0.4']