开发者

The list with most elements

开发者 https://www.devze.com 2023-02-24 05:38 出处:网络
I have a list with dictionary as elements. Each dictionary has an entry called type. The type field represents a list. What is the simplest/pythonic way of obtaini开发者_如何学编程ng the list with the

I have a list with dictionary as elements. Each dictionary has an entry called type. The type field represents a list. What is the simplest/pythonic way of obtaini开发者_如何学编程ng the list with the most elements?

programmes_by_type = []
programmes_by_type.append({'type' : [1, 2, 3]})
programmes_by_type.append({'type' : [2, 5]})
programmes_by_type.append({'type' : [3]})
programmes_by_type.append({'type' : [11, 2, 6, 7]})

Given the previous example it should return the [11, 2, 6, 7] list.


max([option['type'] for option in programmes_by_type], key=len)


max_length = 0
type_list = None

for el in programmes_by_type:
    if len(el) > max_length:
        max_length = len(el)
        type_list = el['type']

type_list now contains the biggest list.

0

精彩评论

暂无评论...
验证码 换一张
取 消