Skip to content

Instantly share code, notes, and snippets.

@Abdurahman-hassan
Last active September 15, 2022 17:48
Show Gist options
  • Save Abdurahman-hassan/768a991994d60b10d3c59b7de22962c4 to your computer and use it in GitHub Desktop.
Save Abdurahman-hassan/768a991994d60b10d3c59b7de22962c4 to your computer and use it in GitHub Desktop.
# List comprehension
# The syntax for list comprehension is:
# [ <expression> for x in <sequence> if <condition>]
data = [2,3,5,7,11,13,17,19,23,29,31]
# Ex1: List comprehension: updating the same list
data = [x+3 for x in data]
print("Updating the list: ", data)
# Updating the list output: [5, 6, 8, 10, 14, 16, 20, 22, 26, 32, 34]
# Ex2: List comprehension: creating a different list with updated values
new_data = [x*2 for x in data]
print("Creating new list: ", new_data)
# Creating new list output: [10, 12, 16, 20, 28, 32, 40, 44, 52, 64, 68]
# Ex3: With an if-condition: Multiples of four:
fourx = [x for x in new_data if x%4 == 0 ]
print("Divisible by four", fourx)
# Divisible by four output: [12, 16, 20, 28, 32, 40, 44, 52, 64, 68]
# Ex4: Alternatively, we can update the list with the if condition as well
fourxsub = [x-1 for x in new_data if x%4 == 0 ]
print("Divisible by four minus one: ", fourxsub)
# Divisible by four minus one output: [11, 15, 19, 27, 31, 39, 43, 51, 63, 67]
# Ex5: Using range function:
nines = [x for x in range(100) if x%9 == 0]
print("Nines: ", nines)
# Nines output: [0, 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99]
# Dictionary comprehension
# The syntax for dict comprehension is:
# dict = { key:value for key, value in <sequence> if <condition> }
# Using range() function and no input list
usingrange = {x:x*2 for x in range(12)}
print("Using range(): ",usingrange)
# Using range() output: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20, 11: 22}
# Lists
months = ["Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"]
number = [1,2,3,4,5,6,7,8,9,10,11,12]
# Using one input list
numdict = {x:x**2 for x in number}
print("Using one input list to create dict: ", numdict)
# Using one input list to create dict output: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144}
# Using two input lists
months_dict = {key:value for (key, value) in zip(number, months)}
print("Using two lists: ", months_dict)
# Using two lists output: {1: 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May', 6: 'June', 7: 'July', 8: 'Aug', 9: 'Sept', 10: 'Oct', 11: 'Nov', 12: 'Dec'}
# Set comprehension
set_a = {x for x in range(10,20) if x not in [12,14,16]}
print(set_a)
# output is: {10, 11, 13, 15, 17, 18, 19}
# Generator comprehension
# Generator comprehensions are also very similar to lists
# with the variation of using curved brackets instead of square brackets.
data = [2,3,5,7,11,13,17,19,23,29,31]
gen_obj = (x for x in data)
print(gen_obj)
# output: <generator object <genexpr> at 0x102a87d60>
print(type(gen_obj))
# output: <class 'generator'>
for items in gen_obj:
print(items, end = " ")
# output: 2 3 5 7 11 13 17 19 23 29 31
# Notice how both map() functions and list comprehension effectively do the same job of modifying iterator sequences
# such as the list in the example above.
def square(num):
return num * 2
# Here is the difference between map() function and list comprehensions:
# but map() functions are still arguably a better choice when it comes to the use of larger sequences.
newdata = map(square, data)
newdata = [x + 3 for x in data]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment