• 회원가입
  • 로그인
  • 구글아이디로 로그인

[howto] Python - 리스트 중복요소 제거. ★ (Remove List Duplicate)

2162  

List 중복요소 제거

[방법1] 바로 적용

 

hzlist = ["HTML", "CSS", "HTML", "JS", "JS"]

hzlist = list(dict.fromkeys(hzlist))

print(hzlist)

 

결과값: ['HTML', 'CSS', 'JS'] 


[방법2] 함수 생성 

 

def hz_function(x):

  return list(dict.fromkeys(x))

hzlist = ["HTML", "CSS", "HTML", "JS", "JS"]

hzlist = hz_function(hzlist)

print(hzlist)

 

결과값: ['HTML', 'CSS', 'JS'] 


PS. 코드 설명

 

dict.fromkeys(hzlist)

hzlist 요소를 key로 사용해 dictionary 생성.

(dictionary는 중복 key 못 갖으므로, 자동으로 중복 요소 제거됨.)

 

list(dict.fromkeys(hzlist))

중복 key 제거된 dictionary를 다시 List로 변환.

 

※ 방법1과 방법2 둘 다 기본 원리는 동일.

 


분류 제목
module Python - cmath.sinh(x) 메서드 -
module Python - cmath.sqrt(x) 메서드 -
module Python - cmath.tan(x) 메서드 -
module Python - cmath.tanh(x) 메서드 -
module Python - cmath.e 상수 - 오일러 상수 (2.71...)
module Python - cmath.inf 상수 - float 자료형의 무한대 상수 (= inf) 반환.
module Python - cmath.infj 상수 - complex 자료형의 무한대 상수 (= infj) 반환.
module Python - cmath.nan 상수 - float 자료형의 nan 상수 (= nan) 반환.
module Python - cmath.nanj 상수 - complex 자료형의 nan 상수 (= nanj) 반환.
module Python - cmath.pi 상수 - 원주율 (= 3.14...) 반환. (= 파이 상수)
module Python - cmath.tau 상수 - 원주율 2배 (= 6.28...) 반환. (= 타우 상수)
howto Python - digit 숫자 (0~9)를 정사각형 형태로 나열.
pandas Python - Pandas Intro (판다스 소개)
pandas Python - Pandas Start (설치/시작/버전확인)
pandas Python - Pandas Series (시리즈)
pandas Python - Pandas DataFrames (데이터프레임)
pandas Python - Pandas Read CSV (CSV 읽기)
pandas Python - Pandas Read JSON (JSON 읽기)
pandas Python - Pandas Analyzing DataFrames (데이터프레임 분석)
pandas Python - Pandas Cleaning Data (데이터 청소/정리)
23/24
목록
찾아주셔서 감사합니다. Since 2012