Iterating over list of dictionaries

后端 未结 4 1133
星月不相逢
星月不相逢 2020-12-02 14:36

I have a list -myList - where each element is a dictionary. I wish to iterate over this list but I am only interesting in one attribute - \'age\' - in each dictionary each

4条回答
  •  不知归路
    2020-12-02 15:13

    The semicolons at the end of lines aren't necessary in Python (though you can use them if you want to put multiple statements on the same line). So it would be more pythonic to omit them.

    But the actual iteration strategy is easy to follow and pretty explicit about what you're doing. There are other ways to do it. But an explicit for-loop is perfectly pythonic.

    (Niklas B.'s answer will not do precisely what you're doing: if you want to do something like that, the format string should be "{0}\n{1}".)

提交回复
热议问题