티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

List vs tuple, when to use each?

리스트와 튜플, 각각 언제 사용해야 할까요?

 문제 내용 

In Python, when should you use lists and when tuples?

파이썬에서 리스트(list)와 튜플(tuple)을 사용하는 경우는 언제인가요?

 

Sometimes you don't have a choice, for example if you have

때로는 선택의 여지가 없습니다. 예를 들어,
"hello %s you are %s years old" % x

 

then x must be a tuple.

이라고 하면 x는 튜플(tuple)이어야 합니다.

 

But if I am the one who designs the API and gets to choose the data types, then what are the guidelines?

하지만 API를 설계하고 데이터 유형을 선택할 수 있는 경우, 지침은 무엇일까요?

 

 

 

 높은 점수를 받은 Solution 

Tuples are fixed size in nature whereas lists are dynamic.
In other words, a tuple is immutable whereas a list is mutable.

튜플은 고정 크기이고, 리스트는 동적입니다.
즉, 튜플은 불변(immutable)하고, 리스트는 가변(mutable)입니다.

 

  1. You can't add elements to a tuple. Tuples have no append or extend method.
  2. You can't remove elements from a tuple. Tuples have no remove or pop method.
  3. You can find elements in a tuple, since this doesn’t change the tuple.
  4. You can also use the in operator to check if an element exists in the tuple.
1. 튜플에는 원소를 추가할 수 없습니다. 튜플에는 append나 extend와 같은 메서드가 없습니다.
2. 튜플에서 원소를 제거할 수 없습니다. 튜플에는 remove나 pop 메서드가 없습니다.
3. 튜플에서 요소를 찾을 수 있습니다. 이 작업은 튜플을 변경하지 않으므로 가능합니다.
4. in 연산자를 사용하여 튜플에 요소가 있는지 확인할 수 있습니다.

 


  • Tuples are faster than lists. If you're defining a constant set of values and all you're ever going to do with it is iterate through it, use a tuple instead of a list.
  • It makes your code safer if you “write-protect” data that does not need to be changed. Using a tuple instead of a list is like having an implied assert statement that this data is constant, and that special thought (and a specific function) is required to override that.
  • Some tuples can be used as dictionary keys (specifically, tuples that contain immutable values like strings, numbers, and other tuples). Lists can never be used as dictionary keys, because lists are mutable.
튜플은 리스트보다 빠릅니다. 상수 집합의 값을 정의하고 이를 반복하는 것이 전부라면 튜플 대신 리스트를 사용하세요.

변경되지 않아야 하는 데이터를 "쓰기 보호"하는 것은 코드를 더 안전하게 만듭니다. 리스트 대신 튜플을 사용하면 이 데이터가 상수임을 나타내는 암시적인 assert 문이 있는 것과 같으며, 이를 무시하려면 특별한 사고(특정 함수)가 필요합니다.

특정한 값(예: 문자열, 숫자, 다른 튜플 등 불변값을 포함하는 튜플)을 포함하는 일부 튜플은 사전 키로 사용할 수 있습니다. 리스트는 절대로 사전 키로 사용될 수 없습니다. 리스트는 가변이기 때문입니다.

 

Source: Dive into Python 3

출처: Dive into Python 3

 

 

 

 가장 최근 달린 Solution 

A minor but notable advantage of a list over a tuple is that lists tend to be slightly more portable. Standard tools are less likely to support tuples. JSON, for example, does not have a tuple type. YAML does, but its syntax is ugly compared to its list syntax, which is quite nice.

리스트에 대한 미묘하지만 주목할 만한 장점 중 하나는 리스트가 약간 더 이식성이 있다는 것입니다. 표준 도구들은 튜플을 지원하는 경우가 적습니다. 예를 들어, JSON에는 튜플 타입이 없습니다. YAML에는 있지만 리스트 구문에 비해 못생겼습니다.

 

In those cases, you may wish to use a tuple internally then convert to list as part of an export process. Alternately, you might want to use lists everywhere for consistency.

이러한 경우 내부적으로 튜플을 사용한 다음 내보내기 프로세스의 일부로 리스트로 변환할 수 있습니다. 또는 일관성을 위해 모든 곳에서 리스트를 사용하고자 할 수 있습니다.

 

 

 

출처 : https://stackoverflow.com/questions/1708510/list-vs-tuple-when-to-use-each

반응형
댓글
공지사항
최근에 올라온 글