Skip to content

30 Days Of Python: Day 5 - Lists

Twitter Follow Author: Asabeneh Yetayeh
Second Edition: July - 2021

<< Day 4 | Day 6 >>

30DaysOfPython

Day 5

Lists

νŒŒμ΄μ¬μ—λŠ” λ„€ 가지 μ»¬λ ‰μ…˜ μžλ£Œν˜•μ΄ μžˆμŠ΅λ‹ˆλ‹€.

  • List: μ •λ ¬λ˜κ³  λ³€κ²½ κ°€λŠ₯(μˆ˜μ • κ°€λŠ₯)ν•œ μ»¬λ ‰μ…˜μž…λ‹ˆλ‹€. 쀑볡 값을 ν—ˆμš©ν•©λ‹ˆλ‹€.
  • Tuple: μ •λ ¬λ˜κ³  λ³€κ²½ λΆˆκ°€λŠ₯ν•˜κ±°λ‚˜ μˆ˜μ • λΆˆκ°€λŠ₯ν•œ(λΆˆλ³€) μ»¬λ ‰μ…˜μž…λ‹ˆλ‹€. 쀑볡 값을 ν—ˆμš©ν•©λ‹ˆλ‹€.
  • Set: μˆœμ„œκ°€ μ§€μ •λ˜μ§€ μ•Šκ³  μΈλ±μŠ€κ°€ μ—†κ³  μˆ˜μ •ν•  수 μ—†λŠ” μ»¬λ ‰μ…˜μ΄μ§€λ§Œ μƒˆ μ•„μ΄ν…œμ„ μΆ”κ°€ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 쀑볡 값은 ν—ˆμš©λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
  • Dictionary: μ •λ ¬λ˜μ§€ μ•Šκ³  λ³€κ²½ κ°€λŠ₯(μˆ˜μ • κ°€λŠ₯)ν•˜λ©° μΈλ±μŠ€κ°€ μžˆλŠ” μ»¬λ ‰μ…˜μž…λ‹ˆλ‹€. 쀑볡 값이 μ—†μŠ΅λ‹ˆλ‹€.

λ¦¬μŠ€νŠΈλŠ” μ •λ ¬λ˜κ³  μˆ˜μ •(λ³€κ²½) κ°€λŠ₯ν•œ λ‹€μ–‘ν•œ μžλ£Œν˜•μ˜ μ»¬λ ‰μ…˜μž…λ‹ˆλ‹€. λͺ©λ‘μ€ λΉ„μ–΄ μžˆκ±°λ‚˜ λ‹€λ₯Έ μžλ£Œν˜• μ•„μ΄ν…œμ„ κ°€μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€.

How to Create a List

νŒŒμ΄μ¬μ—μ„œ λ¦¬μŠ€νŠΈλŠ” 두가지 λ°©λ²•μœΌλ‘œ 생성할 수 μžˆμŠ΅λ‹ˆλ‹€:

  • list λ‚΄μž₯ ν•¨μˆ˜λ₯Ό μ‚¬μš©
# syntax
lst = list()
empty_list = list() # 이건 빈 리슀트 μž…λ‹ˆλ‹€, 리슀트 μ•ˆμ— 아무 값도 μ—†μŠ΅λ‹ˆλ‹€
print(len(empty_list)) # 0
  • λŒ€κ΄„ν˜Έ μ‚¬μš©, []
# syntax
lst = []
empty_list = [] # 이건 빈 리슀트 μž…λ‹ˆλ‹€, 리슀트 μ•ˆμ— 아무 값도 μ—†μŠ΅λ‹ˆλ‹€
print(len(empty_list)) # 0

초기 값이 μžˆλŠ” λ¦¬μŠ€νŠΈμž…λ‹ˆλ‹€. len() 을 μ‚¬μš©ν•˜μ—¬ 리슀트의 길이λ₯Ό μ°ΎμŠ΅λ‹ˆλ‹€.

fruits = ['banana', 'orange', 'mango', 'lemon']                     # list of fruits
vegetables = ['Tomato', 'Potato', 'Cabbage','Onion', 'Carrot']      # list of vegetables
animal_products = ['milk', 'meat', 'butter', 'yoghurt']             # list of animal products
web_techs = ['HTML', 'CSS', 'JS', 'React','Redux', 'Node', 'MongDB'] # list of web technologies
countries = ['Finland', 'Estonia', 'Denmark', 'Sweden', 'Norway']

# Print the lists and its length
print('Fruits:', fruits)
print('Number of fruits:', len(fruits))
print('Vegetables:', vegetables)
print('Number of vegetables:', len(vegetables))
print('Animal products:',animal_products)
print('Number of animal products:', len(animal_products))
print('Web technologies:', web_techs)
print('Number of web technologies:', len(web_techs))
print('Countries:', countries)
print('Number of countries:', len(countries))
output
Fruits: ['banana', 'orange', 'mango', 'lemon']
Number of fruits: 4
Vegetables: ['Tomato', 'Potato', 'Cabbage', 'Onion', 'Carrot']
Number of vegetables: 5
Animal products: ['milk', 'meat', 'butter', 'yoghurt']
Number of animal products: 4
Web technologies: ['HTML', 'CSS', 'JS', 'React', 'Redux', 'Node', 'MongDB']
Number of web technologies: 7
Countries: ['Finland', 'Estonia', 'Denmark', 'Sweden', 'Norway']
Number of countries: 5
  • λ¦¬μŠ€νŠΈλŠ” μ„œλ‘œ λ‹€λ₯Έ μžλ£Œν˜•μ˜ μ•„μ΄ν…œμ„ κ°€μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€.
 lst = ['Asabeneh', 250, True, {'country':'Finland', 'city':'Helsinki'}] # λ‹€λ₯Έ μžλ£Œν˜•μ„ 가진 리슀트

Accessing List Items Using Positive Indexing

인덱슀λ₯Ό μ‚¬μš©ν•˜μ—¬ 리슀트의 각 μ•„μ΄ν…œμ— μ•‘μ„ΈμŠ€ν•©λ‹ˆλ‹€. 리슀트 μΈλ±μŠ€λŠ” 0λΆ€ν„° μ‹œμž‘ν•©λ‹ˆλ‹€. μ•„λž˜ 그림은 μΈλ±μŠ€κ°€ μ‹œμž‘λ˜λŠ” μœ„μΉ˜λ₯Ό λͺ…ν™•ν•˜κ²Œ λ³΄μ—¬μ€λ‹ˆλ‹€. List index

fruits = ['banana', 'orange', 'mango', 'lemon']
first_fruit = fruits[0] # 인덱슀λ₯Ό μ‚¬μš©ν•΄ 첫번째 μ•„μ΄ν…œμ— μ ‘κ·Όν•©λ‹ˆλ‹€
print(first_fruit)      # banana
second_fruit = fruits[1]
print(second_fruit)     # orange
last_fruit = fruits[3]
print(last_fruit) # lemon
# Last index
last_index = len(fruits) - 1
last_fruit = fruits[last_index]

Accessing List Items Using Negative Indexing

음수 μΈλ±μŠ€λŠ” λμ—μ„œ μ‹œμž‘ν•˜λŠ” 것을 μ˜λ―Έν•˜λ©° -1은 λ§ˆμ§€λ§‰ μ•„μ΄ν…œμ„, -2λŠ” λ§ˆμ§€λ§‰μ—μ„œ λ‘λ²ˆμ¨° μ•„μ΄ν…œμ„ μ˜λ―Έν•©λ‹ˆλ‹€.

List negative indexing

fruits = ['banana', 'orange', 'mango', 'lemon']
first_fruit = fruits[-4]
last_fruit = fruits[-1]
second_last = fruits[-2]
print(first_fruit)      # banana
print(last_fruit)       # lemon
print(second_last)      # mango

Unpacking List Items

lst = ['item','item2','item3', 'item4', 'item5']
first_item, second_item, third_item, *rest = lst
print(first_item)     # item1
print(second_item)    # item2
print(third_item)     # item3
print(rest)           # ['item4', 'item5']
# First Example
fruits = ['banana', 'orange', 'mango', 'lemon','lime','apple']
first_fruit, second_fruit, third_fruit, *rest = lst
print(first_fruit)     # banana
print(second_fruit)    # orange
print(third_fruit)     # mango
print(rest)           # ['lemon','lime','apple']
# Second Example about unpacking list
first, second, third,*rest, tenth = [1,2,3,4,5,6,7,8,9,10]
print(first)          # 1
print(second)         # 2
print(third)          # 3
print(rest)           # [4,5,6,7,8,9]
print(tenth)          # 10
# Third Example about unpacking list
countries = ['Germany', 'France','Belgium','Sweden','Denmark','Finland','Norway','Iceland','Estonia']
gr, fr, bg, sw, *scandic, es = countries
print(gr)
print(fr)
print(bg)
print(sw)
print(scandic)
print(es)

Slicing Items from a List

  • μ–‘μˆ˜ 인덱싱: start, end 및 step을 μ§€μ •ν•˜μ—¬ μ–‘μˆ˜ 인덱슀 λ²”μœ„λ₯Ό 지정할 수 μžˆμŠ΅λ‹ˆλ‹€. λ°˜ν™˜ 값은 μƒˆ λ¦¬μŠ€νŠΈκ°€ λ©λ‹ˆλ‹€. (start의 λ””ν΄νŠΈκ°’ = 0, end = len(lst) - 1 (λ§ˆμ§€λ§‰ μ•„μ΄ν…œ), step = 1)
fruits = ['banana', 'orange', 'mango', 'lemon']
all_fruits = fruits[0:4] # λͺ¨λ“  fruitsλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€
# μ΄κ²ƒλ˜ν•œ μœ„μ™€ 같은 값을 λ°˜ν™˜ν•©λ‹ˆλ‹€
all_fruits = fruits[0:] # μš°λ¦¬κ°€ μ–΄λ””μ„œ 멈좜 지 μ„€μ •ν•˜μ§€ μ•ŠμœΌλ©΄, λͺ¨λ“  것을 ν¬ν•¨ν•©λ‹ˆλ‹€
orange_and_mango = fruits[1:3] # 첫번째 인덱슀λ₯Ό ν¬ν•¨ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€
orange_mango_lemon = fruits[1:]
orange_and_lemon = fruits[::2] # μ—¬κΈ°μ„œ μ„Έλ²ˆμ§Έ 인자인 step을 μ‚¬μš©ν–ˆμŠ΅λ‹ˆλ‹€. λͺ¨λ“  λ‘λ²ˆμ§Έ μ•„μ΄ν…œμ„ ν¬ν•¨ν•©λ‹ˆλ‹€ - ['banana', 'mango']
  • 음수 인덱싱: start, end 및 step을 μ§€μ •ν•˜μ—¬ 음수 인덱슀의 λ²”μœ„λ₯Ό 지정할 수 μžˆμŠ΅λ‹ˆλ‹€. λ°˜ν™˜ 값은 μƒˆ λ¦¬μŠ€νŠΈκ°€ λ©λ‹ˆλ‹€.
fruits = ['banana', 'orange', 'mango', 'lemon']
all_fruits = fruits[-4:] # λͺ¨λ“  fruitsλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€
orange_and_mango = fruits[-3:-1] # λ§ˆμ§€λ§‰ 인덱슀λ₯Ό ν¬ν•¨ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€,['orange', 'mango']
orange_mango_lemon = fruits[-3:] # 이것은 -3 λΆ€ν„° μ‹œμž‘ν•˜μ—¬ λκΉŒμ§€μ˜ 값을 μ€λ‹ˆλ‹€,['orange', 'mango', 'lemon']
reverse_fruits = fruits[::-1] # 음수의 step은 리슀트λ₯Ό μ—­μˆœμœΌλ‘œ κ°€μ§‘λ‹ˆλ‹€,['lemon', 'mango', 'orange', 'banana']

Modifying Lists

λ¦¬μŠ€νŠΈλŠ” λ³€κ²½ κ°€λŠ₯ν•˜κ±°λ‚˜ μˆ˜μ • κ°€λŠ₯ν•œ μˆœμ„œκ°€ μžˆλŠ” μ•„μ΄ν…œλ“€μ˜ μ»¬λ ‰μ…˜μž…λ‹ˆλ‹€. 과일 리슀트λ₯Ό μˆ˜μ •ν•΄λ΄…μ‹œλ‹€.

fruits = ['banana', 'orange', 'mango', 'lemon']
fruits[0] = 'avocado'
print(fruits)       #  ['avocado', 'orange', 'mango', 'lemon']
fruits[1] = 'apple'
print(fruits)       #  ['avocado', 'apple', 'mango', 'lemon']
last_index = len(fruits) - 1
fruits[last_index] = 'lime'
print(fruits)        #  ['avocado', 'apple', 'mango', 'lime']

Checking Items in a List

in μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•˜μ—¬ μ•„μ΄ν…œμ΄ 리슀트의 ꡬ성원인지 ν™•μΈν•©λ‹ˆλ‹€. μ•„λž˜ μ˜ˆμ‹œλ₯Ό λ΄…μ‹œλ‹€.

fruits = ['banana', 'orange', 'mango', 'lemon']
does_exist = 'banana' in fruits
print(does_exist)  # True
does_exist = 'lime' in fruits
print(does_exist)  # False

Adding Items to a List

κΈ°μ‘΄ 리슀트의 끝에 μ•„μ΄ν…œμ„ μΆ”κ°€ν•˜λ €λ©΄ append() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€.

# syntax
lst = list()
lst.append(item)
fruits = ['banana', 'orange', 'mango', 'lemon']
fruits.append('apple')
print(fruits)           # ['banana', 'orange', 'mango', 'lemon', 'apple']
fruits.append('lime')   # ['banana', 'orange', 'mango', 'lemon', 'apple', 'lime']
print(fruits)

Inserting Items into a List

insert() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ λͺ©λ‘μ˜ μ§€μ •λœ μΈλ±μŠ€μ— ν•˜λ‚˜μ˜ μ•„μ΄ν…œμ„ μ‚½μž…ν•  수 μžˆμŠ΅λ‹ˆλ‹€. λ‹€λ₯Έ μ•„μ΄ν…œλ“€μ€ 였λ₯Έμͺ½μœΌλ‘œ μ΄λ™ν•œλ‹€λŠ” 것에 μ£Όμ˜ν•©μ‹œλ‹€. insert() λ©”μ„œλ“œλŠ” μΈλ±μŠ€μ™€ μ‚½μž…ν•  μ•„μ΄ν…œμ΄λΌλŠ” 두 가지 인자λ₯Ό κ°€μ§‘λ‹ˆλ‹€.

# syntax
lst = ['item1', 'item2']
lst.insert(index, item)
fruits = ['banana', 'orange', 'mango', 'lemon']
fruits.insert(2, 'apple') # orange와 mango 사이에 apple을 μ‚½μž…
print(fruits)           # ['banana', 'orange', 'apple', 'mango', 'lemon']
fruits.insert(3, 'lime')   # ['banana', 'orange', 'apple', 'lime', 'mango', 'lemon']
print(fruits)

Removing Items from a List

remove λ©”μ„œλ“œλŠ” λ¦¬μŠ€νŠΈμ—μ„œ μ§€μ •λœ μ•„μ΄ν…œμ„ μ‚­μ œν•©λ‹ˆλ‹€.

# syntax
lst = ['item1', 'item2']
lst.remove(item)
fruits = ['banana', 'orange', 'mango', 'lemon', 'banana']
fruits.remove('banana')
print(fruits)  # ['orange', 'mango', 'lemon', 'banana'] - 이 λ©”μ„œλ“œλŠ” λ¦¬μŠ€νŠΈμ—μ„œ 첫번째둜 μ‘΄μž¬ν•˜λŠ” μ•„μ΄ν…œμ„ μ‚­μ œν•©λ‹ˆλ‹€
fruits.remove('lemon')
print(fruits)  # ['orange', 'mango', 'banana']

Removing Items Using Pop

pop() λ©”μ„œλ“œλŠ” μ§€μ •λœ 인덱슀λ₯Ό μ œκ±°ν•©λ‹ˆλ‹€(λ˜λŠ” μΈλ±μŠ€κ°€ μ§€μ •λ˜μ§€ μ•Šμ€ 경우 λ§ˆμ§€λ§‰ μ•„μ΄ν…œ):

# syntax
lst = ['item1', 'item2']
lst.pop()       # λ§ˆμ§€λ§‰ μ•„μ΄ν…œ
lst.pop(index)
fruits = ['banana', 'orange', 'mango', 'lemon']
fruits.pop()
print(fruits)       # ['banana', 'orange', 'mango']

fruits.pop(0)
print(fruits)       # ['orange', 'mango']

Removing Items Using Del

del ν‚€μ›Œλ“œλŠ” μ§€μ •λœ 인덱슀λ₯Ό μ‚­μ œν•˜λ©° 인덱슀 λ²”μœ„ λ‚΄μ˜ μ•„μ΄ν…œμ„ μ‚­μ œν•˜λŠ” 데도 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. λ˜ν•œ 리슀트λ₯Ό μ™„μ „νžˆ μ‚­μ œν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

# syntax
lst = ['item1', 'item2']
del lst[index] # ν•˜λ‹ˆμ˜ μ•„μ΄ν…œ
del lst        # 리슀트λ₯Ό μ™„μ „νžˆ μ‚­μ œ
fruits = ['banana', 'orange', 'mango', 'lemon', 'kiwi', 'lime']
del fruits[0]
print(fruits)       # ['orange', 'mango', 'lemon', 'kiwi', 'lime']
del fruits[1]
print(fruits)       # ['orange', 'lemon', 'kiwi', 'lime']
del fruits[1:3]     # 이것은 주어진 인덱슀 μ‚¬μ΄μ˜ μ•„μ΄ν…œμ„ μ‚­μ œν•©λ‹ˆλ‹€, κ·ΈλŸ¬λ―€λ‘œ μΈλ±μŠ€κ°€ 3인 μ•„μ΄ν…œμ€ μ‚­μ œλ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€!
print(fruits)       # ['orange', 'lime']
del fruits
print(fruits)       # NameError: name 'fruits' is not defined κ°€ λ°œμƒν•΄μ•Όν•©λ‹ˆλ‹€

Clearing List Items

clear() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•΄ 리슀트 λΉ„μš°κΈ°:

# syntax
lst = ['item1', 'item2']
lst.clear()
fruits = ['banana', 'orange', 'mango', 'lemon']
fruits.clear()
print(fruits)       # []

Copying a List

λ‹€μŒμ˜ λ°©λ²•μœΌλ‘œ μƒˆ λ³€μˆ˜μ— μž¬ν• λ‹Ήν•˜μ—¬ 리슀트λ₯Ό 볡사할 수 μžˆμŠ΅λ‹ˆλ‹€:list2 = list1. 이제 list2λŠ” list1의 참쑰이며, list2μ—μ„œ λ³€κ²½ν•œ λ‚΄μš©μ€ 원본 list1도 μˆ˜μ •ν•©λ‹ˆλ‹€. ν•˜μ§€λ§Œ 원본을 μˆ˜μ •ν•˜κ³  싢지 μ•Šκ³  λ‹€λ₯Έ 사본을 κ°–κ³  μ‹Άμ–΄ν•˜λŠ” κ²½μš°κ°€ λ§ŽμŠ΅λ‹ˆλ‹€. μœ„μ˜ 문제λ₯Ό ν”Όν•˜λŠ” ν•œ 가지 방법은 copy() λ₯Ό μ‚¬μš©ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

# syntax
lst = ['item1', 'item2']
lst_copy = lst.copy()
fruits = ['banana', 'orange', 'mango', 'lemon']
fruits_copy = fruits.copy()
print(fruits_copy)       # ['banana', 'orange', 'mango', 'lemon']

Joining Lists

νŒŒμ΄μ¬μ—μ„œ 두 개 μ΄μƒμ˜ λͺ©λ‘μ„ κ²°ν•©ν•˜κ±°λ‚˜ μ—°κ²°ν•˜λŠ” 방법은 μ—¬λŸ¬ 가지가 μžˆμŠ΅λ‹ˆλ‹€.

  • ν”ŒλŸ¬μŠ€ μ—°μ‚°μž (+)
# syntax
list3 = list1 + list2
positive_numbers = [1, 2, 3, 4, 5]
zero = [0]
negative_numbers = [-5,-4,-3,-2,-1]
integers = negative_numbers + zero + positive_numbers
print(integers) # [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
fruits = ['banana', 'orange', 'mango', 'lemon']
vegetables = ['Tomato', 'Potato', 'Cabbage', 'Onion', 'Carrot']
fruits_and_vegetables = fruits + vegetables
print(fruits_and_vegetables ) # ['banana', 'orange', 'mango', 'lemon', 'Tomato', 'Potato', 'Cabbage', 'Onion', 'Carrot']
  • extend() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ μ—°κ²° extend() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜λ©΄ λ¦¬μŠ€νŠΈμ— 리슀트λ₯Ό μΆ”κ°€ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ•„λž˜ 예λ₯Ό μ°Έμ‘°ν•©μ‹œλ‹€.
# syntax
list1 = ['item1', 'item2']
list2 = ['item3', 'item4', 'item5']
list1.extend(list2)
num1 = [0, 1, 2, 3]
num2= [4, 5, 6]
num1.extend(num2)
print('Numbers:', num1) # Numbers: [0, 1, 2, 3, 4, 5, 6]
negative_numbers = [-5,-4,-3,-2,-1]
positive_numbers = [1, 2, 3,4,5]
zero = [0]

negative_numbers.extend(zero)
negative_numbers.extend(positive_numbers)
print('Integers:', negative_numbers) # Integers: [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
fruits = ['banana', 'orange', 'mango', 'lemon']
vegetables = ['Tomato', 'Potato', 'Cabbage', 'Onion', 'Carrot']
fruits.extend(vegetables)
print('Fruits and vegetables:', fruits ) # Fruits and vegetables: ['banana', 'orange', 'mango', 'lemon', 'Tomato', 'Potato', 'Cabbage', 'Onion', 'Carrot']

Counting Items in a List

count() λ©”μ„œλ“œλŠ” λ¦¬μŠ€νŠΈμ— μ•„μ΄ν…œμ΄ λ‚˜νƒ€λ‚˜λŠ” 횟수λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€:

# syntax
lst = ['item1', 'item2']
lst.count(item)
fruits = ['banana', 'orange', 'mango', 'lemon']
print(fruits.count('orange'))   # 1
ages = [22, 19, 24, 25, 26, 24, 25, 24]
print(ages.count(24))           # 3

Finding Index of an Item

index() λ©”μ„œλ“œλŠ” λ¦¬μŠ€νŠΈμ— μžˆλŠ” μ•„μ΄ν…œμ˜ 인덱슀λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€:

# syntax
lst = ['item1', 'item2']
lst.index(item)
fruits = ['banana', 'orange', 'mango', 'lemon']
print(fruits.index('orange'))   # 1
ages = [22, 19, 24, 25, 26, 24, 25, 24]
print(ages.index(24))           # 2, 처음 λ§Œλ‚œ 것

Reversing a List

reverse() λ©”μ„œλ“œλŠ” 리슀트의 μˆœμ„œλ₯Ό 거꾸둜 ν•©λ‹ˆλ‹€.

# syntax
lst = ['item1', 'item2']
lst.reverse()
fruits = ['banana', 'orange', 'mango', 'lemon']
fruits.reverse()
print(fruits) # ['lemon', 'mango', 'orange', 'banana']
ages = [22, 19, 24, 25, 26, 24, 25, 24]
ages.reverse()
print(ages) # [24, 25, 24, 26, 25, 24, 19, 22]

Sorting List Items

리슀트λ₯Ό μ •λ ¬ν•˜λ €λ©΄ sort() λ©”μ„œλ“œ λ˜λŠ” sorted() λ‚΄μž₯ ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. sort() λ©”μ„œλ“œλŠ” 리슀트 μ•„μ΄ν…œμ„ μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ •λ ¬ν•˜κ³  μ›λž˜ 리슀트λ₯Ό μˆ˜μ •ν•©λ‹ˆλ‹€. λ§Œμ•½ sort() λ©”μ„œλ“œμ˜ reverse의 μΈμžκ°€ true라면, 그것은 λͺ©λ‘μ„ λ‚΄λ¦Όμ°¨μˆœμœΌλ‘œ λ°°μ—΄ν•  κ²ƒμž…λ‹ˆλ‹€.

  • sort(): 이 λ©”μ„œλ“œλŠ” μ›λž˜ 리슀트λ₯Ό μˆ˜μ •ν•©λ‹ˆλ‹€
# syntax
lst = ['item1', 'item2']
lst.sort()                # μ˜€λ¦„μ°¨μˆœ
lst.sort(reverse=True)    # λ‚΄λ¦Όμ°¨μˆœ

Example:

fruits = ['banana', 'orange', 'mango', 'lemon']
fruits.sort()
print(fruits)             # μ•ŒνŒŒλ²³μˆœμœΌλ‘œ μ •λ ¬, ['banana', 'lemon', 'mango', 'orange']
fruits.sort(reverse=True)
print(fruits) # ['orange', 'mango', 'lemon', 'banana']
ages = [22, 19, 24, 25, 26, 24, 25, 24]
ages.sort()
print(ages) #  [19, 22, 24, 24, 24, 25, 25, 26]

ages.sort(reverse=True)
print(ages) #  [26, 25, 25, 24, 24, 24, 22, 19]

sorted(): μ›λž˜ 리슀트λ₯Ό μˆ˜μ •ν•˜μ§€ μ•Šκ³  μ •λ ¬λœ 리슀트λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€ Example:

fruits = ['banana', 'orange', 'mango', 'lemon']
print(sorted(fruits))   # ['banana', 'lemon', 'mango', 'orange']
# μ—­μˆœ
fruits = ['banana', 'orange', 'mango', 'lemon']
fruits = sorted(fruits,reverse=True)
print(fruits)     # ['orange', 'mango', 'lemon', 'banana']

πŸŒ• 당신은 μ„±μ‹€ν•˜κ³  이미 λ§Žμ€ 것을 μ„±μ·¨ν–ˆμŠ΅λ‹ˆλ‹€. μ—¬λŸ¬λΆ„μ€ 이제 막 5일차 도전을 마쳀고 μœ„λŒ€ν•¨μ„ ν–₯ν•œ 5걸음 μ•žμ— μžˆμŠ΅λ‹ˆλ‹€. 이제 μ—¬λŸ¬λΆ„μ˜ λ‡Œμ™€ κ·Όμœ‘μ„ μœ„ν•œ μš΄λ™μ„ ν•˜μ„Έμš”.

πŸ’» Exercises: Day 5

Exercises: Level 1

  1. 빈 리슀트λ₯Ό μ„ μ–Έν•©λ‹ˆλ‹€
  2. 5개 μ΄μƒμ˜ μ•„μ΄ν…œμ„ κ°–λŠ” 리슀트λ₯Ό μ„ μ–Έν•©λ‹ˆλ‹€
  3. λ‹Ήμ‹ μ˜ 리슀트의 길이λ₯Ό μ•Œμ•„λ΄…λ‹ˆλ‹€
  4. 리슀트의 첫번째, μ€‘κ°„μ˜, λ§ˆμ§€λ§‰ μ•„μ΄ν…œμ„ μ–»μ–΄λ΄…λ‹ˆλ‹€
  5. mixed_data_types λΌλŠ” 리슀트λ₯Ό μ„ μ–Έν•˜κ³ , λ‹Ήμ‹ μ˜ 이름, λ‚˜μ΄, ν‚€, 결혼 μ—¬λΆ€, μ£Όμ†Œλ₯Ό λ„£μ–΄λ΄…μ‹œλ‹€
  6. it_companies λΌλŠ” μ΄λ¦„μ˜ λͺ©λ‘ λ³€μˆ˜λ₯Ό μ„ μ–Έν•˜κ³  초기 값에 Facebook, Google, Microsoft, Apple, IBM, Oracle 및 Amazon을 ν• λ‹Ήν•©λ‹ˆλ‹€
  7. print() λ₯Ό μ‚¬μš©ν•˜μ—¬ 리슀트λ₯Ό ν”„λ¦°νŠΈ ν•©λ‹ˆλ‹€
  8. λ¦¬μŠ€νŠΈμ— μžˆλŠ” κΈ°μ—… 수λ₯Ό ν”„λ¦°νŠΈ ν•©λ‹ˆλ‹€
  9. 첫번째, 쀑간, λ§ˆμ§€λ§‰ 기업을 ν”„λ¦°νŠΈ ν•©λ‹ˆλ‹€
  10. κΈ°μ—… 쀑 ν•˜λ‚˜λ₯Ό μˆ˜μ •ν•˜κ³  리슀트λ₯Ό ν”„λ¦°νŠΈ ν•©λ‹ˆλ‹€
  11. it_companies 에 ν•˜λ‚˜μ˜ IT 기업을 μΆ”κ°€ν•©λ‹ˆλ‹€
  12. νšŒμ‚¬ 리슀트 쀑간에 IT 기업을 μΆ”κ°€ν•©λ‹ˆλ‹€
  13. it_companies 이름 쀑 ν•˜λ‚˜λ₯Ό λŒ€λ¬Έμžλ‘œ λ³€κ²½ν•©λ‹ˆλ‹€ (IBM μ œμ™Έ!)
  14. '#;  ' λΌλŠ” λ¬Έμžμ—΄λ‘œ it_companies 에 μ—°κ²°ν•©λ‹ˆλ‹€
  15. it_companies λ¦¬μŠ€νŠΈμ— νŠΉμ • 기업이 μ‘΄μž¬ν•˜λŠ” 지 ν™•μΈν•©λ‹ˆλ‹€
  16. sort() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•΄ 리슀트λ₯Ό μ •λ ¬ν•©λ‹ˆλ‹€
  17. reverse() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ 리슀트λ₯Ό λ‚΄λ¦Όμ°¨μˆœμœΌλ‘œ λ°˜μ „ν•©λ‹ˆλ‹€
  18. λ¦¬μŠ€νŠΈμ—μ„œ 처음 3개의 기업을 μž˜λΌλƒ…λ‹ˆλ‹€
  19. λ¦¬μŠ€νŠΈμ—μ„œ λ§ˆμ§€λ§‰ 3개의 기업을 μž˜λΌλƒ…λ‹ˆλ‹€
  20. λ¦¬μŠ€νŠΈμ—μ„œ μ€‘κ°„μ˜ IT κΈ°μ—… λ˜λŠ” 기업듀을 μž˜λΌλƒ…λ‹ˆλ‹€
  21. λ¦¬μŠ€νŠΈμ—μ„œ 첫번째 IT 기업을 μ‚­μ œν•©λ‹ˆλ‹€
  22. λ¦¬μŠ€νŠΈμ—μ„œ μ€‘κ°„μ˜ IT κΈ°μ—… λ˜λŠ” 기업듀을 μ‚­μ œν•©λ‹ˆλ‹€
  23. λ¦¬μŠ€νŠΈμ—μ„œ λ§ˆμ§€λ§‰ IT 기업을 μ‚­μ œν•©λ‹ˆλ‹€
  24. λ¦¬μŠ€νŠΈμ—μ„œ λͺ¨λ“  IT 기업을 μ‚­μ œν•©λ‹ˆλ‹€
  25. IT κΈ°μ—… 리슀트λ₯Ό μ™„μ „νžˆ μ œκ±°ν•©λ‹ˆλ‹€
  26. λ‹€μŒ 리슀트λ₯Ό μ—°κ²°ν•©λ‹ˆλ‹€:

    front_end = ['HTML', 'CSS', 'JS', 'React', 'Redux']
    back_end = ['Node','Express', 'MongoDB']
    
  27. 26번 문제의 리슀트λ₯Ό μ—°κ²°ν•œ ν›„, μ—°κ²°λœ 리슀트λ₯Ό 볡사해 full_stack λ³€μˆ˜μ— ν• λ‹Ήν•©λ‹ˆλ‹€. 그리고 Python, SQL, Reduxλ₯Ό μ‚½μž…ν•©λ‹ˆλ‹€.

Exercises: Level 2

  1. λ‹€μŒμ€ 10λͺ…μ˜ ν•™μƒμ˜ λ‚˜μ΄ λ¦¬μŠ€νŠΈμž…λ‹ˆλ‹€:
ages = [19, 22, 19, 24, 20, 25, 26, 24, 25, 24]
  • 리슀트λ₯Ό μ •λ ¬ν•˜κ³  μ΅œμ†Œκ°’ 및 μ΅œλŒ€κ°’ μ°ΎμŠ΅λ‹ˆλ‹€
  • λ¦¬μŠ€νŠΈμ— μ΅œμ†Œκ°’ 및 μ΅œλŒ€κ°’μ„ λ‹€μ‹œ μΆ”κ°€ν•©λ‹ˆλ‹€
  • λ‚˜μ΄μ˜ μ€‘μœ„κ°’μ„ μ°ΎμŠ΅λ‹ˆλ‹€(쀑간 μ•„μ΄ν…œ ν•˜λ‚˜ λ˜λŠ” 쀑간 μ•„μ΄ν…œ 두 개λ₯Ό 2둜 λ‚˜λˆˆ κ°’)
  • 평균 λ‚˜μ΄λ₯Ό κ΅¬ν•©λ‹ˆλ‹€(λͺ¨λ“  μ•„μ΄ν…œμ˜ 합을 개수둜 λ‚˜λˆˆ κ°’)
  • λ‚˜μ΄μ˜ λ²”μœ„λ₯Ό κ΅¬ν•©λ‹ˆλ‹€(μ΅œλŒ€κ°’ λΉΌκΈ° μ΅œμ†Œκ°’)
  • (μ΅œμ†Œκ°’ - 평균)κ³Ό (μ΅œλŒ€κ°’ - 평균)의 값을 λΉ„κ΅ν•˜κ³  abs() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€.

  • κ΅­κ°€ λͺ©λ‘ μ—μ„œ 쀑간 κ΅­κ°€λ₯Ό μ°ΎμŠ΅λ‹ˆλ‹€.

  • κ΅­κ°€ 리슀트λ₯Ό λ‘κ°œμ˜ 리슀트둜 λ‚˜λˆ•λ‹ˆλ‹€. 짝수라면 λ‘κ°œμ˜ 리슀트의 크기가 κ°–κ²Œ, μ•„λ‹ˆλΌλ©΄ μ•žμ˜ λ¦¬μŠ€νŠΈκ°€ ν•˜λ‚˜μ˜ κ΅­κ°€λ₯Ό 더 갖도둝 ν•©λ‹ˆλ‹€.
  • ['China', 'Russia', 'USA', 'Finland', 'Sweden', 'Norway', 'Denmark']. μ•žμ˜ μ„Έκ°œ ꡭ가와 λ‚˜λ¨Έμ§€λ₯Ό scandic countries둜 unpackν•©λ‹ˆλ‹€.

πŸŽ‰ CONGRATULATIONS ! πŸŽ‰

<< Day 4 | Day 6 >>