본문 바로가기
Python/Python

int형 list를 .join으로 꺼내기

by Sondho 2020. 5. 5.

int형으로 구성된 list를 .join하려고하면 아래와 같은 에러가 발생한다.

number = [0, 1, 2, 3, 4, 5]
print(" ".join(number))


# 실행결과
예외가 발생했습니다. TypeError
sequence item 0: expected str instance, int found
File "G:\test.py", line 2, in <module>
    print(" ".join(number))

 

이를 해결하려면 str형으로 바꿔줘야하는데 map()을 사용하면 쉽게 해결할 수 있다.

number = [0, 1, 2, 3, 4, 5]
print(" ".join(map(str, number)))


# 실행결과
0 1 2 3 4 5

 

 

댓글