kopia lustrzana https://github.com/animator/learn-python
Create list_comprehension.md
Added Introduction Added Syntax Added Examples Added Conclusionpull/891/head
rodzic
e06ac86031
commit
ea37cbb254
|
@ -0,0 +1,73 @@
|
|||
# List Comprehension
|
||||
|
||||
Creating lists concisely and expressively is what list comprehension in Python does. You can generate lists from already existing iterables like lists, tuples or strings with a short form.
|
||||
This boosts the readability of code and reduces necessity of using explicit looping constructs.
|
||||
|
||||
## Syntax :
|
||||
|
||||
### Basic syntax
|
||||
|
||||
```python
|
||||
new_list = [expression for item in iterable]
|
||||
```
|
||||
- **new_list**: This is the name given to the list that will be created using the list comprehension.
|
||||
- **expression**: This is the expression that defines how each element of the new list will be generated or transformed.
|
||||
- **item**: This variable represents each individual element from the iterable. It takes on the value of each element in the iterable during each iteration.
|
||||
- **iterable**: This is the sequence-like object over which the iteration will take place. It provides the elements that will be processed by the expression.
|
||||
|
||||
This list comprehension syntax `[expression for item in iterable]` allows you to generate a new list by applying a specific expression to each element in an iterable.
|
||||
|
||||
### Syntax including condition
|
||||
|
||||
```python
|
||||
new_list = [expression for item in iterable if condition]
|
||||
```
|
||||
- **new_list**: This is the name given to the list that will be created using the list comprehension.
|
||||
- **expression**: This is the expression that defines how each element of the new list will be generated or transformed.
|
||||
- **item**: This variable represents each individual element from the iterable. It takes on the value of each element in the iterable during each iteration.
|
||||
- **iterable**: This is the sequence-like object over which the iteration will take place. It provides the elements that will be processed by the expression.
|
||||
- **if condition**: This is an optional part of the syntax. It allows for conditional filtering of elements from the iterable. Only items that satisfy the condition
|
||||
will be included in the new list.
|
||||
|
||||
|
||||
## Examples:
|
||||
|
||||
1. Generating a list of squares of numbers from 1 to 5:
|
||||
|
||||
```python
|
||||
squares = [x ** 2 for x in range(1, 6)]
|
||||
print(squares)
|
||||
```
|
||||
|
||||
- **Output** :
|
||||
```python
|
||||
[1, 4, 9, 16, 25]
|
||||
```
|
||||
|
||||
2. Filtering even numbers from a list:
|
||||
|
||||
```python
|
||||
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
even = [x for x in nums if x % 2 == 0]
|
||||
print(even)
|
||||
```
|
||||
|
||||
- **Output** :
|
||||
```python
|
||||
[2, 4, 6, 8, 10]
|
||||
```
|
||||
|
||||
3. Flattening a list of lists:
|
||||
```python
|
||||
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
|
||||
flat = [x for sublist in matrix for x in sublist]
|
||||
print(flat)
|
||||
```
|
||||
|
||||
- **Output** :
|
||||
```python
|
||||
[1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
```
|
||||
|
||||
List comprehension is a powerful feature in Python for creating lists based on existing iterables with a concise syntax.
|
||||
By mastering list comprehension, developers can write cleaner, more expressive code and leverage Python's functional programming capabilities effectively.
|
Ładowanie…
Reference in New Issue