본문 바로가기
코딩 연습/파이썬 기초(예제)

파이썬 리스트, 튜플, 딕셔너리, 세트 등에 대해 알아보기

by good4me 2023. 12. 2.

goodthings4me.tistory.com

파이썬 리스트, 튜플, 딕셔너리, 세트

 

빅데이터 분석이나 머신러닝 등에 가장 많이 사용하는 언어인 파이썬 문법을 배울 때 다른 언어에 비해 재미있는 부분이 바로 파이썬 리스트, 튜플, 딕셔너리, 세트 자료형입니다. 이 자료형들을 스터디하면서 전체적으로 다시 정리를 해보았네요.

 

파이썬 리스트, 튜플, 딕셔너리, 세트 개념 알아보기

파이썬의 데이터 타입 중에서 컬렉션이라고 하는 list, tuple, dict, set 여러 개의 요소를 담을 수 있는 자료형을 의미합니다.

 

 

  • List (리스트): 순서가 있는 변경 가능한(mutable) 시퀀스. 여러 가지 데이터 타입의 요소를 담을 수 있습니다.
  • Tuple (튜플): 순서가 있는 변경 불가능한(immutable) 시퀀스. 리스트와 비슷하지만 요소를 수정할 수 없습니다.
  • Dictionary (딕셔너리): 키-값 쌍으로 이루어진 순서가 없는 컬렉션. 각 키는 유일해야 하며, 값은 중복될 수 있습니다.
  • Set (세트): 순서가 없고 중복이 허용되지 않는 집합. 주로 집합 연산과 리스트, 튜플의 중복 제거시 유용하게 사용됩니다.

이러한 컬렉션들은 파이썬에서 데이터를 구조화하고 효과적으로 다룰 수 있도록 도와줍니다

 

iterable이란?

  • 반복적으로 값을 가져올 수 있는 값들을 가지고 있는 객체로, 여러 개의 요소를 가지고 있다보니 반복문(for문, while문)을 통해 순차적으로 값에 접근할 수 있는 객체를 의미합니다.
  • 이러한 iterable 객체로는 리스트(list), 튜플(tuple), 세트(set), 사전(dict), 문자열(string)이 있으며, 이들 모두 반복 가능한(iterable) 객체입니다.
  • 반면에 정수(int), 실수(float), 불리언(boolean)과 같은 객체는 단일 값을 가지기 때문에 반복 가능한 객체가 아닙니다.

 

리스트, 튜플, 딕셔너리, 세트의 메서드 확인하기

 

IT, 전자제품 선물

 

 

list tuple dict set
.append .count .clear .clear
.clear .index .copy .add
.copy   .fromkeys .copy
.count   .get .difference
.extend   .items .difference_update
.index   .keys .discard
.insert   .pop .intersection
.pop   .popitem .intersection_update
.remove   .setdefault .isdisjoint
.reverse   .update .issubset
.sort   .values .issuperset
      .pop
      .remove
      .symetric_difference
      .symetric_difference_update
      .union
      .update

 

 

파이썬 리스트 (List)

  • 여러 개의 자료를 하나로 묶어 사용할 수 있는 자료형으로 파이썬에서 가장 많이 쓰임
  • 대괄호([ ])로 표현하고 대괄호 내 각각의 요소를 쉼표로 구분함

VS Code에서 리스트 메서드 확인하기
VS Code에서 리스트 메서드 확인하기

 

 

▶ 리스트 생성

## List, 리스트 생성
lst1 = []
lst2 = list()
print(f'리스트 생성1: {lst1}, 리스트 생성2: {lst2}')
# 리스트 생성1: [], 리스트 생성2: []

lst1 = [1, 2, 3]
print(lst1)
# [1, 2, 3]

lst2 = ['a', 'b', 'c', '가나다']
print(lst2)
# ['a', 'b', 'c', '가나다']

lst3 = [1, 3.14, 'a', True, print]
print(lst3)
# [1, 3.14, 'a', True, ]

lst4 = [lst3, 'a', 'b', 'c', True]
print(lst4)
# [[1, 3.14, 'a', True, ], 'a', 'b', 'c', True]

 

 

 리스트 연산: 더하기, 곱하기

## 더하기
list1 = [1, 2, 3]
list2 = [4, 5, 6]
print(list1 + list2)
# [1, 2, 3, 4, 5, 6]

## 곱하기 -  더하기 2번 하는 개념 (수학에서 더하기와 곱하기의 연관 의미를 생각해요!)
print(list1 * 2)
# [1, 2, 3, 1, 2, 3]

print(list2 * 3)
# [4, 5, 6, 4, 5, 6, 4, 5, 6]

 

 

 리스트 인덱싱(indexing)

## List 인덱싱(indexing)
items = ['H', 'e', 'l', 'l', 'o', ' ', 'P', 'y', 't', 'h', 'o', 'n']
print(items[0])
# H
print(items[6])
# P
print(items[-1])
# n
print(items[-8])
# o

 

 

 리스트 슬라이싱(slicing)

## List 슬라이싱(slicing)
print(items[0:])
# ['H', 'e', 'l', 'l', 'o', 'P', 'y', 't', 'h', 'o', 'n']
print(items[6:])
# ['P', 'y', 't', 'h', 'o', 'n']
print(items[:5])
# ['H', 'e', 'l', 'l', 'o']
print(items[:-4])
# ['H', 'e', 'l', 'l', 'o', ' ', 'P', 'y']
print(items[-6:-4])
# ['P', 'y']

 

 

 리스트 값 추출

## List 값 추출 - 중첩 리스트
items = ['a', 'b', 'c', [1, 2, 3], '가', '나', '다']
print(items[2:5])
# ['c', [1, 2, 3], '가']
print(items[3])
# [1, 2, 3]
print(items[3][1])
# 2
print(items[3][:])
# [1, 2, 3]
print(items[3][:-1])
# [1, 2]
items = ['a', [1, 2, ['Life', 'is', 'too', 'short']], '가']
print(items[1])
# [1, 2, ['Life', 'is', 'too', 'short']]
print(items[1][1])
# 2
print(items[1][2])
# ['Life', 'is', 'too', 'short']
print(items[1][2][2])
# too

 

 

 리스트 값 추가 - append(x)

## append(x) 함수 - 리스트의 끝에 요소 x 추가함
items = [1, 2]
items.append('가')
print(items)
# [1, 2, '가']

itm = [4, 5]
items.append(itm)
print(items)
# [1, 2, '가', [4, 5]]

 

 

 리스트 값 추가 - extend(iterable)

## extend(iterable) 함수 - iterable의 모든 요소를 추가할 수 있음 (시퀀스 자료형을 같은 형태로 만들어서 추가함)
items = [1, 2]
items.extend('가')
print(items)
# [1, 2, '가']

itm = [5, 6]
items.extend(itm)
print(items)
# [1, 2, '가', 5, 6]

items.extend('abc')
print(items)
# [1, 2, '가', 5, 6, 'a', 'b', 'c']

# items.extend(7)  # TypeError: 'int' object is not iterable : 시퀀스 자료형이 아니기 때문에 에러가 남
# print(items)

 

 

 리스트 값 추가 - insert(i, x)

## insert(i, x) 함수 - 리스트의 지정된 위치(index)에 요소 x를 삽입함. 맨 앞(0)에 넣는 경우에 많이 사용, 가장 뒤에 넣는 경우는 append() 사용
items = [1, 2]
items.insert(2, '가')
print(items)
# [1, 2, '가']

itm = [5]
items.insert(1, itm)
print(items)
# [1, [5], 2, '가']

itm = 'Insert'
items.insert(100, itm)  # index 100이 없지만 insert되며, 맨 마지막에 저장됨. 중간 4 ~ 99 index는 없음
print(items)
# [1, [5], 2, '가', 'Insert']

print(items.index('Insert'))
# 4

 

 

 리스트 값 변경

## List 값 변경
items = [1, 2, 3, 4, 5]
items[0] = 10
print(items)
# [10, 2, 3, 4, 5]

items[2:4] = [30, 40]
print(items)
# [10, 2, 30, 40, 5]

items[2:4] = [30, 40, 50, 60]  # 슬라이싱으로 값을 변경할 경우, 지정한 2:4는 변경할 위치의 범위만을 지정하는 것임 
print(items)
# [10, 2, 30, 40, 50, 60, 5]

 

 

 리스트 값 삭제 - del, remove(x), pop(), pop([i])

## del
items = [1, 2, '삭제', 3, 4, 5, 6]
del items[2]
print(items)
# [1, 2, 3, 4, 5, 6]

## remove(x): 리스트에서 첫 번째로 나오는 x값을 제거함
items.remove(3)
print(items)
# [1, 2, 4, 5, 6]

## pop([i]): index i번째의 요소를 제거하고 그 값을 반환하며, index 미 지정 시 맨 마지막 요소 삭제
last = items.pop()
print(last, items)
# 6 [1, 2, 4, 5]

items = [1, 2, 3, 4]
print(items.pop(2))  # 숫자 요소 2가 아니라 index 2번째인 3을 제거하고 반환함
# 3
print(items)
# [1, 2, 4]

 

 

 리스트 관련 함수 - index(x)

## index(x): 특정 값 x의 첫 번째 위치에 대한 인덱스를 반환함
items = ['P', 'y', 't', 'h', 'o', 'n']
print(items.index('y'))
# 1
print(items.index('n'))
# 5
# print(items.index('X'))  # 에러 ValueError: 'X' is not in list

 

 

리스트 관련 함수 - count(x)

## count(x): 특정 값 x 개수(횟수)를 반환함
items = [1, 1, 3, 2, 4, 3, 2, 4, 4]
print(items.count(4))
# 3
print(items.count(3))
# 2

 

 

리스트 관련 함수 - sort()  cf)sorted()

## sort() - 정렬된 결과를 반환하지 않고 원 리스트만 수정됨. 반환값이 없거나 None 반환 
# 형식) (method) def sort(*, key: None = None, reverse: bool = False) -> None
items = ['p', 'y', 't', 'h', 'o', 'n']
items.sort()
print(items)  # 오름차순
# ['h', 'n', 'o', 'p', 't', 'y']

items.sort(reverse=True)
print(items)  # 내림차순
# ['y', 't', 'p', 'o', 'n', 'h']

## Tip) 원래의 리스트를 변경하지 않고 정렬된 새로운 리스트를 얻고 싶다면 sorted() 함수를 사용함
items = [3, 1, 4, 1, 5, 9, 2]
sorted_list = sorted(items)
print(items)  # [3, 1, 4, 1, 5, 9, 2]
print(sorted_list)  # [1, 1, 2, 3, 4, 5, 9]
sorted_list_reverse = sorted(items, reverse=True)
print(sorted_list_reverse)  # [9, 5, 4, 3, 2, 1, 1]

 

 

리스트 관련 함수 - reverse()

## reverse() - 리스트의 순서를 뒤집음
items = [10, 2, 30, 40, 5]
items.reverse()
print(items)
# [5, 40, 30, 2, 10]

 

 

리스트 관련 함수 - clear()

## clear(): 리스트의 모든 요소를 제거함
items = [1, 2, 3]
items.clear()
print(items) 
# 출력: []

 

 

리스트 연습

people = [['kim', 20, ['서울', '구로구']], ['park', 30, ['부산', '금정구']]]
print(f'Name: {people[0][0]} {people[1][0]}')
print(f'Age: {people[0][1]} {people[1][1]}')
print(f'City: {people[0][2][0]} {people[1][2][0]}')

# Name: kim park
# Age: 20 30
# City: 서울 부산


items = ['사과', '바나나', '배', '포도']
items.reverse()
print(items)
# ['포도', '배', '바나나', '사과']



## 예제)
items = ['비행기', '바나나', '파인애플', '감사합니다', '우영우']
items.sort(key=lambda x: x[0])
print(items)
# ['감사합니다', '바나나', '비행기', '우영우', '파인애플']

items.sort(key=lambda x: x[1])
print(items)
# ['바나나', '감사합니다', '우영우', '파인애플', '비행기']

items1 = sorted(items, key=lambda x: x[1])
print(items1)
# ['바나나', '감사합니다', '우영우', '파인애플', '비행기']

 

 

파이썬 튜플 (Tuple)

  • 리스트와 같이 서로 다른 여러 개의 자료(요소)를 묶어서 담고 그것을 하나로 사용할 수 있는 자료형
  • 리스트와 같이 순서(Sequence)와 인덱스를 사용하여 접근 가능
  • 리스트와 다른 점은 요소의 추가, 변경, 삭제가 불가능
  • 튜플은 불변을 기본으로 하기 때문에 이런 특징이 필요한 곳에 사용하면 유용한다.

VS Code에서 튜플 메서드 확인하기
VS Code에서 튜플 메서드 확인하기

 

 

튜플 생성

## 튜플 생성
tuple1 = ()
tuple2 = tuple()
print(tuple1, tuple2)
# () ()

tuple3 = (1,)
print(tuple3)
# (1,)

tuple4 = 1,
print(tuple4)
# (1,)

 

 

 튜플 생성, 연산, 인덱싱

# 튜플은 요소(element)의 추가, 변경, 삭제가 불가능하지만, 다음은 가능하다. 
# - 더하거나 곱하기가 가능한 것은 새로운 튜플이 생성되기 때문임

tpl1 = (1, 2)
tpl2 = (3, 4)

## 튜플 더하기
# - '+' 연산자로 두 튜플을 연결하여 새로운 튜플 생성
print(tpl1 + tpl2)
# (1, 2, 3, 4)

# 튜플 곱하기
# - '*' 연산자로 튜플의 요소를 반복하여 새로운 튜플을 생성 (2개의 튜플을 곱하는 것은 불가능)
print(tpl1 * 2)
# (1, 2, 1, 2)

## 튜플 인덱싱
tpl3 = (1, 2, 3, 4, 5)
print(tpl3[3])
# 4
print(tpl3[1:-1])
# (2, 3, 4)

 

 

 튜플 관련 함수 - count(), index()

## count() - 특정 요소의 개수 반환
tpl1 = (1, 1, 2, 3, 3, 4, 4, 4)
print(tpl1.count(4))
# 3

tpl2 = (1, 2, 3, 'a', 'b', 'c')
print(len(tpl2))
# 6

## index() - 특정 요소의 위치(index) 반환
tpl4 = ('apple', 'orange', 'banana', 'apple')
print(tpl4.index('banana'))
# 2

 

 

 튜플을 리스트로 변환하기

## 튜플을 리스트로 변환하기
lst1 = list(tpl4)
print(lst1)
# ['apple', 'orange', 'banana', 'apple']

## 리스트를 다시 튜플로 변환하기
tpl5 = tuple(lst1)
print(tpl5)
# ('apple', 'orange', 'banana', 'apple')

 

 

파이썬 사전 타입 딕셔너리 (Dictionary)

  • 딕셔너리는 키와 값으로 여러 데이터 타입을 하나로 묶어서 사용할 수 있는 자료형으로,
  • 딕셔너리라고 부르지만 사용할 때 표기는 dict로 하며, 중괄호({ })로 표현하고, 콜론(:)으로 키와 값을 분리하여 표시하고, 쉼표(,)로 각각의 요소를 구분함
  • set와 같은 중괄호로 표시는 하나 set는 키와 값으로 표시하지 않기 때문에 dict와 구분이 되며,
  • 딕셔너리의 키(key)는 문자, 정수, 실수, 튜플이 가능하나 일반적으로 문자 또는 정수만 사용하고, 값(value)은 모든 자료형이 가능함

VS Code에서 딕셔너리 메서드 확인하기
VS Code에서 딕셔너리 메서드 확인하기

 

IT관련서비스제품

 

딕셔너리 생성

## Dictionary 생성 (dict로 표기)
dict1 = {}
dict2 = dict()
print(dict1, dict2)
# {} {}

 

 

딕셔너리 표기 방식

dict1 = {'name': 'kim'}
print(dict1)
# {'name': 'kim'}

dict2 = {123: 456}
print(dict2)
# {123: 456}

dict3 = {'name': 'kim', 'age': 20}
print(dict3)
# {'name': 'kim', 'age': 20}

dict4 = {'name': 'kim', 'age': 20, 'city': ['서울시', '구로구']}
print(dict4)
# {'name': 'kim', 'age': 20, 'city': ['서울시', '구로구']}

dict5 = {'dict': {'name': 'kim', 'age': 20}}
print(dict5)
# {'dict': {'name': 'kim', 'age': 20}}

dict6 = dict([['name','kim'], ['age', 20]])
print(dict6)
# {'name': 'kim', 'age': 20}

dict7 = dict((['name','kim'], ['age', 20]))
print(dict7)
# {'name': 'kim', 'age': 20}

dict8 = dict((('name','kim'), ('age', 20)))
print(dict8)
# {'name': 'kim', 'age': 20}

 

 

 딕셔너리 추가, 변경, 삭제

## 요소(element) 추가 - 키(key)와 값(value)을 쌍으로 하여 추가함
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict1['d'] = 4
print(dict1)
# {'a': 1, 'b': 2, 'c': 3, 'd': 4}

## 요소(element) 변경 - 특정 키의 값을 수정함
dict1['c'] = 30
print(dict1)
# {'a': 1, 'b': 2, 'c': 30, 'd': 4}

## 요소(element) 추가와 변경 동시 수행하기 - update()
# 한번에 많은 키와 값 추가 시 유용
dict1.update({'c': 3, 'd': 40, 'e': 5})
print(dict1)
# {'a': 1, 'b': 2, 'c': 3, 'd': 40, 'e': 5}

## 요소(element) 삭제 - pop(), del
dict1.pop('a')
print(dict1)
# {'b': 2, 'c': 3, 'd': 40, 'e': 5}

print(dict1.pop('c'))  # pop()은 삭제 후 값 반환
# 3

del dict1['d']
print(dict1)
# {'b': 2, 'e': 5}

 

 

 딕셔너리 키, 값 추출하기

## indexing - key를 기준으로 값 추출
dict1 = {'a': 10, 'b': 20, 'c': 30 }
print(dict1['a'])
# 10
print(dict1['c'])
# 30

dict1[3.14] = '파이'
dict1[1] = 'int'
dict1[2] = 'str'
print(dict1['a'])
# 10
print(dict1[3.14])
# 파이
print(dict1[2])
# str

## 딕셔너리 키 추출하기 - keys 함수
print(dict1.keys)  # <built-in method keys of dict object at 0x000001EAF28EB680>
print(dict1.keys())
# dict_keys(['a', 'b', 'c', 3.14, 1, 2])

## 딕셔너리 값 추출하기 - values 함수
print(dict1.values())
# dict_values([10, 20, 30, '파이', 'int', 'str'])

## 딕셔너리 키와 값 전체 추출 - items 함수
print(dict1.items())
# dict_items([('a', 10), ('b', 20), ('c', 30), (3.14, '파이'), (1, 'int'), (2, 'str')])

 

 

 딕셔너리 키와 값을 list, tuple로 변환

## 딕셔너리의 키와 값을 list로 변환
print(list(dict1.keys()))
# ['a', 'b', 'c', 3.14, 1, 2]
print(list(dict1.values()))
# [10, 20, 30, '파이', 'int', 'str']
print(list(dict1.items()))
# [('a', 10), ('b', 20), ('c', 30), (3.14, '파이'), (1, 'int'), (2, 'str')]

## 딕셔너리의 키와 값을 tuple로 변환
print(tuple(dict1.keys()))
# ('a', 'b', 'c', 3.14, 1, 2)
print(tuple(dict1.values()))
# (10, 20, 30, '파이', 'int', 'str')
print(tuple(dict1.items()))
# (('a', 10), ('b', 20), ('c', 30), (3.14, '파이'), (1, 'int'), (2, 'str'))

 

 

파이썬 집합 자료형 세트 (Set)

  • 집합(Set)을 생성하기 위한 함수로, 여러 개의 자료형을 하나로 묶어 사용할 수 있는 자료형
  • 다른 자료형과 다르게 숫자, 문자, 튜플만 저장할 수 있음
  • 딕셔너리와 같은 중괄호({ })로 표현하고 쉼표로 각각의 요소를 구분함
  • 값의 순서가 없고, 요소의 중복(값)을 허용하지 않음

VS Code에서 세트 메서드 확인하기
VS Code에서 세트 메서드 확인하기

 

 

 세트(Set) 생성

## 딕셔너리와 같은 {}를 사용하기 때문에 {}는 사용 못하나, {}에 요소 값이 있을 경우는 사용 가능
set1 = set()
print(set1)
# set()

# set2 = set(1, 2, 3)  # TypeError: set expected at most 1 argument, got 3 --> set()에는 1개의 인자만 들어가야 함
set2 = set([1, 2, 3])
print(set2)
# {1, 2, 3}

set3 = {2, 4, 7, 4, 6, 1, 'a', (4, 14)}
# set3 = {2, 4, 7, 4, 6, 1, 'a', (4, [14, 15])}  # TypeError: unhashable type: 'list'
print(set3)
# {1, 2, (4, 14), 4, 6, 7, 'a'}

# set3 = set([1, 2, 3], 3)  # TypeError: set expected at most 1 argument, got 2
set3 = set([1, 2, 3])
print(set3)
# {1, 2, 3}

# set2 = {5, '가', [2, 6, 1]}  # TypeError: unhashable type: 'list'

set4 = set([1, 1, 1, 2, 2, 3])
print(set4)
# {1, 2, 3}

 

 

세트(Set)  추가, 삭제 (*추출, 변경은 불가능)

## set 값은 추가 가능하나 index 추출 또는 변경 불가능
set5 = {3, 10, 2, 5}

# set에 값 추가 - 추가된 값의 순서는 모름
set5.add(6)
print(set5)

# index로 값 추출
# print(set5[2])  # TypeError: 'set' object is not subscriptable

# index로 값 변경 
# set5[1] = 20  # TypeError: 'set' object does not support item assignment
# print(set5)

## set 값 추가 - update()
set5.update([7, 'a', '가'])
print(set5)

## set 값 삭제 - pop() remove()
set6 = {7, '가', 10, 'a'}
print(set6.pop())  # pop() 추출은 가능하나 어떤 값이 추출되는지는 모르는 문제가 있음

set6.remove('a')
print(set6)

 

 

세트(Set) 집합 함수 다루기

## 교집합
a = set([1, 2, 3, 4, 5])
b = set([3, 4, 5, 6, 7])
print(a & b)
# {3, 4, 5}
print(a.intersection(b))
# {3, 4, 5}

## 합집합
print(a | b)
# {1, 2, 3, 4, 5, 6, 7}
print(a.union(b))
# {1, 2, 3, 4, 5, 6, 7}

## 차집합
print(a - b)
# {1, 2}
print(a.difference(b))
# {1, 2}
print(b - a)
# {6, 7}
print(b.difference(a))
# {6, 7}


## list, tuple에서 중복할 때 유용함
items = (1, 2, 3, 4, 1, 2, 3)
s = set(items)
items2 = tuple(s)
print(items2)
# (1, 2, 3, 4)

 

 

세트(Set)는 list, tuple에서 중복 요소 제거할 때 유용함

## list, tuple에서 중복 제거
items = (1, 2, 3, 4, 1, 2, 3)
s = set(items)
items2 = tuple(s)
print(items2)
# (1, 2, 3, 4)

 

 

 

파이썬 연산자, 변수, 자료형 알아보기

파이썬의 문법은 연산자, 변수, 자료형으로 구성되고, 그 외의 구성 요소로는 조건과 반복을 다루는 제어문, 예약어와 식별자, 함수, 클래스, 라이브러리, 모듈 등이 있는데, 본 포스팅에서 파이

goodthings4me.tistory.com

 

댓글