0% found this document useful (0 votes)
1 views1 page

Doc6 Python One Liners

Uploaded by

Swetha Arendla
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views1 page

Doc6 Python One Liners

Uploaded by

Swetha Arendla
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python One-Liners & Tips

Data Structures
# List comprehension with filter
evens = [x for x in range(100) if x % 2 == 0]

# Dictionary from two lists


d = dict(zip(keys, values))

# Flatten nested list


flat = [item for sublist in nested for item in sublist]

# Count occurrences
from collections import Counter
counts = Counter(words)

File I/O
# Read entire file
content = open('[Link]').read()

# Read lines into list


lines = open('[Link]').read().splitlines()

# Write JSON
import json; [Link](data, open('[Link]', 'w'), indent=2)

Useful Tricks
# Swap variables
a, b = b, a

# Merge dictionaries
merged = {**dict1, **dict2}

# Ternary
result = "yes" if condition else "no"

# Multiple assignment
x = y = z = 0

© 2026

You might also like