This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = min(len(names), len(colors)) | |
for i in range(n): | |
print names[i], colors[i] | |
for names, color in zip(names, colors): | |
print name, color | |
#collections.izip is the iterator version |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for i in range(len(colors)): | |
print i, colors[i] | |
for i, colors in enumerate(colors): | |
print i, colors | |
# retuns enumerate object, not a list |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
matrix = [[1,2,3]] * 4 | |
[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]] | |
# FLATTEN THE MATRIX | |
flatten = [] | |
for row in matrix: | |
for i in range(3): | |
flatten.append(row[i]) | |
[row[i] for row in matrix for i in range(3)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[x**2 for x in (2,4,6)] | |
{x: x**2 for x in (2, 4, 6)} | |
{x for x in 'abracadabra' if x not in 'abc'} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try: | |
f = open(filename, 'r') | |
except IOError as e: | |
print 'cannot open ', filename | |
print "I/O error({0}): {1}".format(e.errno, e.strerror) | |
except: | |
log.exception("Unexpected error") | |
# print "Unexpected error:", sys.exc_info()[0] | |
raise | |
else: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'lmnop' in 'abcdefghijklmnopqrstuvwxyz' | |
index = 'python'.find('on') # 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if x % 2 == 0: | |
return True | |
return False | |
bool(x % 2 == 0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for x in numbers: | |
if x % 2 != 0: | |
return False | |
return True | |
all(x % 2 == 0 for x in numbers) | |
# it's a generator, stops at the first occurrence |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for x in numbers: | |
if x % 2 == 0: | |
return True | |
return False | |
any(x % 2 == 0 for x in numbers) | |
# it's a generator, stops at the first occurrence |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dict(izip(keys, values)) | |
# reuses the same tuple! |