Skip to content

Latest commit

 

History

History
46 lines (39 loc) · 1019 Bytes

README.md

File metadata and controls

46 lines (39 loc) · 1019 Bytes

Useful-Python-Tips

Sort By Key

data = [{"id": 4, "text": "hi"}, 
        {"id": 3, "text": "hi"},
        {"id": 1, "text": "hi"},
        {"id": 5, "text": "hi"},
        {"id": 2, "text": "hi"}]
data.sort(key=lambda x: x["id"], reverse=False)    
# Check if you broke out of loop, else do something. 
# (Typically you would set a flag to check if you completed a loop or not)

for i in range(10):
  print(i)
  if i == 9:
    break
else:
  print("We went through the entire loop!")
for n in range(2, 10):
    for x in range(2, n):
        if n % x == 0:
            print( n, 'equals', x, '*', n/x)
            break
    else:
        # loop fell through without finding a factor
        print(n, 'is a prime number')
a = [1,2]
b = 45
print([b, a]) # [45, [1,2]]

# But
print([b, *a]) # [45, 1, 2]