# for + append 사용
result = []
for i in range(10):
result.append(i)
print(result)
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# List Comprehension 사용
result = [i for i in range(10)]
print(result)
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# 필터
result = [i for i in range(10) if i % 2 == 0]
print(result)
=> [0, 2, 4, 6, 8]
Nested For loop
word_1 = "Hello"
word_2 = "World"
result = [i+j for i in word_1 for j in word_2]
print(result)
=> ['HW', 'Ho', 'Hr', 'Hl', 'Hd',
'eW', 'eo', 'er', 'el', 'ed',
'lW', 'lo', 'lr', 'll', 'ld',
'lW', 'lo', 'lr', 'll', 'ld',
'oW', 'oo', 'or', 'ol', 'od']
Nested For loop + filter
# Nested For loop
case_1 = ["A","B","C"]
case_2 = ["D","E","A"]
result = [i+j for i in case_1 for j in case_2]
print(result)
=> ['AD', 'AE', 'AA', 'BD', 'BE', 'BA', 'CD', 'CE', 'CA']
# Nested For loop + filter
# Filter: i랑 j과 같다면 List에 추가하지 않음
result = [i+j for i in case_1 for j in case_2 if not(i==j)]
print(result)
=> ['AD', 'AE', 'BD', 'BE', 'BA', 'CD', 'CE', 'CA']
result.sort()
print(result)
=> ['AD', 'AE', 'BA', 'BD', 'BE', 'CA', 'CD', 'CE']
# 문장을 빈칸 기준으로 나눠 list로 변환
words = 'The quick brown fox jumps over the lazy dog'.split()
print (words)
=> ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
# list의 각 elemente들을 대문자, 소문자, 길이로 변환하여 two dimensional list로 변환
stuff = [[w.upper(), w.lower(), len(w)] for w in words]
for i in stuff:
print (i)
['THE', 'the', 3]
['QUICK', 'quick', 5]
['BROWN', 'brown', 5]
['FOX', 'fox', 3]
['JUMPS', 'jumps', 5]
['OVER', 'over', 4]
['THE', 'the', 3]
['LAZY', 'lazy', 4]
['DOG', 'dog', 3]