Skip to content

Commit cfbc50a

Browse files
authored
Merge pull request #33 from inishchith/master
list comprehension . close #25
2 parents b652e82 + 8ef91a1 commit cfbc50a

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

python/06-Lists.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,35 @@ li = ["c" ,"d" , "e" , "a" ,"b" ]
197197

198198
li.sort() # li is now ["a", "b" , "c" , "d" , "e"]
199199
```
200+
201+
### List Comprehension
202+
203+
> List comprehensions provide a concise way to create / manipulate lists . For example,,
204+
205+
206+
```python
207+
student_names = ["dustin" , "mark" ,"adrian" ,"thomas"]
208+
209+
initials = []
210+
# creating a list of initails of student names
211+
for student in student_names:
212+
initials.append(student[0])
213+
214+
print(initials) # >>> [ 'd', 'm','a', 't']
215+
216+
# However this can be done in a better way
217+
initials = [student[0] for student in student_names]
218+
219+
print(initials) # >>> [ 'd', 'm','a', 't']
220+
221+
# similarly
222+
name_length = [len(student) for student in student_names]
223+
224+
print(name_length) # >>> [6, 4 ,6 ,6]
225+
226+
# list of squares
227+
228+
squares = [i*i for i in range(1,5)]
229+
print(squares) # >>> [1, 4 ,9 , 16 , 25]
230+
231+
```

python/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
10. [User defined functions](./10-user_defined_functions.md)
1616
11. [Lambda](./11-Lambda.md)
1717
12. [Modules](./12-Modules.md)
18+
13. [Error Handling]()

0 commit comments

Comments
 (0)