美文网首页
Comprehensions in Python

Comprehensions in Python

作者: 游文影月志 | 来源:发表于2020-07-24 05:57 被阅读0次

Comprehension is a very powerful technique to make complicated things simple in Python.

This article will talk about list comprehensions, dictionary comprehensions, and set comprehensions in Python.

1. Python List Comprehension

Suppose, we want to separate the letters of the word Hello and add the letters as items of a list.

We usually use a for loop to complete it:

a = []

for letter in 'Hello':
    a.append(letter)

print(a)  # => ['H', 'e', 'l', 'l', 'o']

List comprehension is an elegant way to define and create lists based on existing lists.

相关文章

网友评论

      本文标题:Comprehensions in Python

      本文链接:https://www.haomeiwen.com/subject/bwudlktx.html