Comprehensions
# Create a list
list1 = ['a', 'b', 'c']
# Create a comprehension
list2 = [x for x in list1]
list3 = [x for x in list1 if x == 'a']
list4 = [x for x in range(5)]
# Generate values in hex
list5 = [hex(x) for x in range(5)]
list6 = [hex(x) if x > 0 else "X" for x in range(5)]
list7 = [x * x for x in range(5)]
list8 = [x for x in range(5) if x == 0 or x == 1]
list9 = [[1,2,3],[4,5,6],[7,8,9]]
list10 = [y for x in list9 for y in x]
list11 = [c for c in "string"]
# Use join
print("".join(list11))
Last updated