
In this lecture, we will learn about type conversion between data structures.
Table of Contents(目次)
What Is Type Conversion of Data Structures — Purpose and Benefits
When writing code, there are situations where you may want to convert data into a different data structure.
For example, since a tuple cannot be edited, it must be converted into a list if you need to modify its elements.
Similarly, if you want to remove duplicate elements from a list, you can write concise code like the second line below. This works because a set inherently does not allow duplicate values. (However, keep in mind that once you convert it into a set, the order of elements is no longer guaranteed.)
duplicate_list = [5, 4, 2, 3, 1, 2, 4, 5]
unique_list = list(set(duplicate_list))
print(unique_list) # [1, 2, 3, 4, 5] (* Order is not guaranteed)
Without using a set, writing code to eliminate duplicates would be much more complex and would significantly increase Python’s processing time. By contrast, a set performs duplicate elimination efficiently, and the greatest benefit is that you can write concise code in just one line.
Other cases where you may want to convert to different data structures include:
- Making data easier to handle
- Since set does not guarantee element order, convert it into a list or tuple to manage indexes
- Writing simpler code through conversion
- Using methods available in the target data structure can reduce the number of code lines
- Making code more concise and readable
- Eliminating duplicate elements in a list using
list(set(duplicate_list))
- Eliminating duplicate elements in a list using
- Converting a tuple to a list to enable editing
- Converting a list to a tuple when you want to prevent modifications
- Even if code accidentally attempts to modify an element, it will raise an error during execution
1. Methods for Converting Between Sequence Types in Python
1-1. Conversion Between list and tuple (Mutable ⇔ Immutable)
Use the tuple()
function to convert into a tuple. Use the list()
function to convert into a list.
When to use:
- When you want immutability →
tuple()
- When you want mutability →
list()
l1 = [1, 2, 3]
t1 = tuple(l1) # (1, 2, 3)
t2 = (4, 5, 6)
l2 = list(t2) # [4, 5, 6]
1-2. Conversion from list/ tuple to set (Duplicate Removal and Set Operations)
Use the set()
function to convert a list or tuple into a set. Remember that once converted into a set, element order is not guaranteed.
When to use:
- When you want to remove duplicates
- When performing set operations (union, intersection, difference)
l = [1, 2, 2, 3]
s1 = set(l) # {1, 2, 3}
t = (3, 3, 4)
s2 = set(t) # {3, 4}
1-3. Conversion from set to list/ tuple (Restoring Order)
By converting a set into a list or tuple, you can handle it as an ordered sequence. However, note that the element order immediately after conversion is not guaranteed.
When to use:
- When you need to convert a set into an ordered sequence
- When performing index-based operations
s = {1, 3, 2}
l = list(s) # [1, 2, 3] # Order is not guaranteed
t = tuple(s) # (1, 2, 3) # Order is not guaranteed
2. Methods for Converting Between Strings and Other Data Types in Python
2-1. Conversion from String (str) to list/ tuple/ set
You can convert a str into a list, tuple, or set. In this case, each character in the string is extracted and added as an element. If converted into a set, duplicate characters are removed and order is not preserved.
Additionally, by using the split()
method of str, you can obtain a list of substrings split by a delimiter. For example, running split()
with the delimiter (','
) on the string 'Nico,Tom,Emma'
will return a list of words separated by commas.
When to use:
- When processing by individual characters
- When splitting into a list by delimiter
s = 'hello'
chars_list = list(s) # ['h', 'e', 'l', 'l', 'o']
chars_tuple = tuple(s) # ('h', 'e', 'l', 'l', 'o')
chars_set = set(s) # {'e', 'h', 'o', 'l'}
text = 'Nico,Tom,Emma'
parts = text.split(',') # ['Nico', 'Tom', 'Emma']
2-2. Conversion from list/ tuple/ set to String (str)
To convert a list, tuple, or set into a str, use the str.join()
method.
The join()
method is invoked on the delimiter string you want to insert between elements. For example, if the delimiter is a comma (','
), you would write ','.join(seq)
. If the delimiter is an empty string (''
), the elements are concatenated directly in sequence.
Note that each element must be a string. Unlike format
, join()
does not automatically convert values into strings. If the elements include numbers or other non-string values, it will result in an error. Therefore, ensure all elements are converted to strings beforehand.
When to use:
- When you want to combine elements of a list or set into a single string
- For CSV or log output
chars = ['a', 'b', 'c']
s1 = ''.join(chars) # 'abc'
s2 = ','.join(chars) # 'a,b,c'
t = ('x', 'y', 'z')
s3 = ' & '.join(t) # 'x & y & z'
s4 = ''.join({'a', 'b', 'c'}) # 'cba' # Order is not guaranteed for sets
s5 = ''.join(sorted({'a', 'b', 'c'})) # 'abc' # Use sorted() to enforce order
3. Methods for Converting Between dict and Other Data Types in Python
3-1. Conversion from dict to list/ tuple/ set
You can convert a dict into a list, tuple, or set.
To extract keys, simply pass the dict itself as an argument, e.g., list(d)
.
To extract values, use list(d.values())
.
While keys in a dict are unique, values may contain duplicates. By converting to a set (set(d.values())
), you can obtain unique values.
If you convert d.items()
using list(d.items())
, you will obtain data containing (key, value) pairs, e.g., [('a', 1), ('b', 2), ('c', 1)]
.
When to use:
- When you want to extract keys or values for sequence operations
- When you need a unique set of values
d = {'a': 1, 'b': 2, 'c': 1}
keys_list = list(d) # ['a', 'b', 'c']
values_list = set(d.values()) # {1, 2}
items_list = list(d.items()) # [('a', 1), ('b', 2), ('c', 1)]
3-2. Conversion from (key, value) List to dict
You can create a dict from a list or tuple in the form [('key1', 'value1'), ('key2', 'value2')]
. Use the dict()
function to convert into a dict.
When to use:
- When creating a dict from paired data, such as databases or CSV files
pairs = [('a', 1), ('b', 2)]
d = dict(pairs) # {'a': 1, 'b': 2}