강의

멘토링

커뮤니티

Inflearn Community Q&A

chungstupid0975's profile image
chungstupid0975

asked

Programming for Us: Intermediate Python (Inflearn Original)

리스트 반복

Written on

·

261

0

안녕하세요

[['~']*2]가 [['~'], ['~']]가 아니고 [['~', '~']]가 출력이 되는지 궁금합니다.

또 [['~']*2]와 [['~'] for n in range(2)] 가 다른 이유도 모르겠습니다. 

pythondjango

Answer 1

0

niceman님의 프로필 이미지
niceman
Instructor

네 답변드릴께요

[['2'] * 2 ] == [['2'] + ['2']]  즉, 단일 리스트 원소 추가 후 결과 반환입니다.

list 컴프리 핸션의 경우에는 [['2']] * 2 가 되어서 리스트 자체가 복사 된 것입니다. 

감사합니다.

chungstupid0975's profile image
chungstupid0975

asked

Ask a question