Skip to content

Instantly share code, notes, and snippets.

@MacTechIN
Created September 20, 2023 16:59
Show Gist options
  • Save MacTechIN/61f8570a4152778a875f6a062531b866 to your computer and use it in GitHub Desktop.
Save MacTechIN/61f8570a4152778a875f6a062531b866 to your computer and use it in GitHub Desktop.
Python list conprehension examples
#
# a = []
# for i in range(1, 10+1):
# if i % 2 == 1:
# a.append(i*2)
#
# print (a)
# List comprehension type by python
print([x * 2 for x in range(1, 10 + 1) if x % 2 == 1])
#
# a = {}
#
# for key,value in original.items():
# a[key] = value
#
#아래 코드로 간략하게 표현 가능하다.
original = {1:"a", 2:"b"}
my_dict = {key:value for key, value in original.items() }
print(my_dict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment