Skip to content

snphan/Useful-Python-Tips

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

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]

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published