-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathislice.py
More file actions
31 lines (18 loc) · 744 Bytes
/
Copy pathislice.py
File metadata and controls
31 lines (18 loc) · 744 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from itertools import islice, count
abc = 'A B C D E F G H I J K L'
c = abc.split()
print(c[2:8:2])
print(list(islice(c, 2, 8, 2)))
# An infinite iterator
numbers = count()
# This would run forever if we tried to materialize it into a list
# print(list(numbers)[1000:1005]) # This would not work
# islice works fine with the infinite iterator
print(list(islice(numbers, 1000, 1005)))
def large_file_reader():
for i in range(1000000): # Simulating reading a large file
yield f"Line {i}"
# Regular slicing would require loading all lines into memory
# print(list(large_file_reader())[10000:10005]) # Memory intensive
# islice processes only the required elements
print(list(islice(large_file_reader(), 10000, 10005)))