Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Convert Tuple to List

#1
Convert Tuple to List

Problem: Given a Python tuple with n elements. How to convert it into a list with the same n elements?

Examples:

  • Convert tuple (1, 2, 3, 4, 5) into list [1, 2, 3, 4, 5].
  • Convert tuple ('Alice', 'Bob', 'Ann') into list ['Alice', 'Bob', 'Ann'].
  • Convert tuple (1,) into list [1].

Note Tuple: Tuples are similar to lists—with the difference that you cannot change the tuple values (tuples are immutable) and you use parentheses rather than square brackets.

Solution: Use the built-in Python list() function to convert a list into a tuple. You don’t need to import any external library.

Code: The following code converts the three given tuples into lists.

tuple_1 = (1, 2, 3, 4, 5)
print(list(tuple_1))
# [1, 2, 3, 4, 5] tuple_2 = ('Alice', 'Bob', 'Ann')
print(list(tuple_2))
# ['Alice', 'Bob', 'Ann'] tuple_3 = (1,)
print(list(tuple_3))
# [1]

Try It Yourself: With our interactive code shell, you can try it yourself. As a small exercise, try to convert the empty tuple () into a list and see what happens.

Explanation: You can see that converting a tuple with one element leads to a list with one element. The list() function is the easiest way to convert a tuple into a list. Note that the values in the tuple are not copied—only a new reference to the same element is created:


The graphic also shows how to convert a tuple back to a list by using the tuple() function (that’s also a Python built-in function). Thus, calling list(tuple(lst)) on a list lst will result in a new list with the same elements.

Related articles:

Try to execute this code with the interactive Python tutor:



https://www.sickgaming.net/blog/2020/04/...e-to-list/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Python IndexError: Tuple Index Out of Range [Easy Fix] xSicKxBot 0 1,950 08-22-2023, 09:07 AM
Last Post: xSicKxBot
  [Tut] Python Tuple Concatenation: A Simple Illustrated Guide xSicKxBot 0 1,940 08-21-2023, 10:25 AM
Last Post: xSicKxBot
  [Tut] Sort a List, String, Tuple in Python (sort, sorted) xSicKxBot 0 1,695 08-15-2023, 02:08 PM
Last Post: xSicKxBot
  [Tut] Easiest Way to Convert List of Hex Strings to List of Integers xSicKxBot 0 1,458 11-25-2022, 11:54 AM
Last Post: xSicKxBot
  [Tut] Python Find Shortest List in List xSicKxBot 0 1,372 09-25-2022, 03:42 AM
Last Post: xSicKxBot
  [Tut] Python Find Longest List in List xSicKxBot 0 1,302 09-23-2022, 02:19 PM
Last Post: xSicKxBot
  [Tut] Python Set to Tuple | Tuple to Set | 3 Easy Ways xSicKxBot 0 1,193 09-21-2022, 10:33 PM
Last Post: xSicKxBot
  [Tut] How to Create a Python Tuple of Size n? xSicKxBot 0 1,212 08-26-2022, 10:02 AM
Last Post: xSicKxBot
  [Tut] How to Convert a List of Dicts to a CSV File in Python [4 Ways] xSicKxBot 0 1,183 08-13-2022, 04:32 AM
Last Post: xSicKxBot
  [Tut] How to Convert a List of Objects to a CSV File in Python [5 Ways] xSicKxBot 0 1,119 07-30-2022, 10:14 PM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016