• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Convert List into Tuple Using Python

Convert List into Tuple Using Python

July 13, 2022 Leave a Comment

To convert a list into a tuple using Python, use the tuple() function.

lst = ['h', 'e', 'l', 'l' ,'o']

t = tuple(lst)

print(t)

#Output:
('h', 'e', 'l', 'l', 'o')

When working with different objects in Python, the ability to be able to convert objects into other objects can be valuable.

One such situation is if you want to convert a list to a tuple.

To convert a list into a tuple using Python, use the tuple() function.

Below is a simple example of how you can convert a list into a tuple using tuple() in Python.

lst = ['h', 'e', 'l', 'l' ,'o']

t = tuple(lst)

print(t)

#Output:
('h', 'e', 'l', 'l', 'o')

Convert Tuple to List in Python

To go the other way and convert a tuple into a list, you can use the Python list() function in the same way as we used the tuple() function above.

Below is a simple example of how you can convert a tuple into a list in Python.

t = ('h', 'e', 'l', 'l', 'o')

lst = list(t)

print(lst)

#Output:
['h', 'e', 'l', 'l' ,'o']

Using tuple() to Convert Other Objects to Tuples in Python

You can also use the Python tuple() function to convert other objects into tuples.

One such example is if you want to convert a string into a tuple in your Python code.

To do so, you can slightly modify the example from above.

Below is a simple example showing you how to convert a string into a tuple using Python.

string = "hello"

t = tuple(string)

print(t)

#Output:
('h', 'e', 'l', 'l', 'o')

Hopefully this article has been useful for you to learn how to convert a list into a tuple in Python.

Other Articles You'll Also Like:

  • 1.  Using Python to Add Trailing Zeros to String
  • 2.  Using Python to Remove Non-Empty Directory
  • 3.  Create List of Odd Numbers in Range with Python
  • 4.  How to Save and Use Styles from Existing Word File With python-docx
  • 5.  Get pandas Series First Element in Python
  • 6.  Get First Key and Value in Dictionary with Python
  • 7.  Using Python to Get Queue Size
  • 8.  Reverse String with Recursion in Python
  • 9.  Using Python Selenium Webdriver to Refresh Page
  • 10.  Check if Variable is Tuple in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy