l **=** ['a', 'b', 'c']
t **=** ('a', 'b', 'c')
d **=** {'a': 1, 'b': 2, 'c': 3}
s **=** 'abc'
l.reverse() *# list의 순서를 뒤집어줌*
t.reverse() *# AttributeError: 'tuple' object has no attribute 'reverse'*
d.reverse() *# AttributeError: 'dict' object has no attribute 'reverse'*
s.reverse() *# AttributeError: 'str' object has no attribute 'reverse'*
l **=** ['a', 'b', 'c']
l_reverse **=** l.reverse()
**print**(l_reverse) *# None*
**print**(l) *# ['c', 'b', 'a']*
즉, 리스트 값울 추가로 생성하지 않고 바로 뒤집어준다는 것이다.
l **=** ['a', 'b', 'c']
t **=** ('a', 'b', 'c')
d **=** {'a': 1, 'b': 2, 'c': 3}
s **=** 'abc'
reversed(l) *# <listreverseiterator object at 0x101053c10>*
reversed(t) *# <reversed object at 0x101053b50>*
reversed(d) *# TypeError: argument to reversed() must be a sequence*
reversed(s) *# <reversed object at 0x101053c10>*
보다시피 딕셔너리는 시퀀셜한 타입이 아니므로 지원하지 않는다.
즉, l[0], l[1] 과 같이 순차적인 인덱스로 접근할 수 없기떄문에 지원하지 않는다.
l **=** ['a', 'b', 'c']
t **=** ('a', 'b', 'c')
s **=** 'abc'
reversed(l) *# <listreverseiterator object at 0x101053c10>*
reversed(t) *# <reversed object at 0x101053b50>*
reversed(s) *# <reversed object at 0x101053c10>*
tuple과 str의 경우 ‘reversed’ 객체를 반환하는것을 볼 수 있다.
하지만 list의 경우 특이하게 ‘listreverseiterator’를 반환하고있다.
사용 방법에 차이는 없으므로 크게 신경쓰지 않아도 된다.
reversed 객체를 tuple 혹은 list로 바꾸어 사용해주려면 다음과 같이 하면 된다.
l **=** ['a', 'b', 'c']
t **=** ('a', 'b', 'c')
list(reversed(l)) *# ['c', 'b', 'a']*
tuple(reversed(t)) *# ('c', 'b', 'a')*
물론, list로 만든 listreverseiterator 객체를 반드시 list로 만들어야 되는 것은 아니고,
tuple로 만든 reversed 객체를 반드시 tuple로 만들어야하는건 아니다.
문자열로 만들려면 굳이 list나 tuple로 바꿀 필요 없이 join을 통해 요소들을 연결해주면 된다.
l **=** ['a', 'b', 'c']
''.join(reversed(l)) *# 'cba'*