GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — Convert NumPy array to Python tuple and print

Convert NumPy array to Python tuple and Print

Last update on September 01 2025 12:42:30 (UTC/GMT +8 hours)


4. Array to Tuple Conversion

Write a NumPy program to convert a NumPy array to a Python tuple and print the tuple.

Sample Solution:

Python Code:

import numpy as np

# Initialize a NumPy array
numpy_array = np.array([1, 2, 3, 4, 5])
print("Original NumPy array:",numpy_array)
print("Type:",type(numpy_array))

# Convert the NumPy array to a Python tuple
python_tuple = tuple(numpy_array)

print("\nNumPy array to a Python tuple:")
# Print the Python tuple
print(python_tuple)
print("Type:",type(python_tuple))

Output:

Original NumPy array: [1 2 3 4 5]
Type: <class 'numpy.ndarray'>

NumPy array to a Python tuple:
(1, 2, 3, 4, 5)
Type: <class 'tuple'>

Explanation:

  • Importing numpy: We first import the numpy library for array manipulations.
  • Initializing a NumPy array: A NumPy array is initialized with some elements.
  • Converting to Python tuple: The NumPy array is converted to a Python tuple using the tuple() function.
  • Printing the tuple: The resulting Python tuple is printed.

For more Practice: Solve these Related Problems:

  • Write a Numpy program to convert a multidimensional NumPy array into a nested Python tuple while preserving the structure.
  • Write a Numpy program to convert a structured NumPy array into a tuple of tuples, ensuring each inner tuple represents a row.
  • Write a Numpy program to convert a NumPy array with custom dtypes into a tuple and verify that element types match the original array.
  • Write a Numpy program to convert a transposed NumPy array into a tuple and then reverse the operation to regain the original array.

Go to:

  • NumPy Interoperability Exercises Home ↩
  • NumPy Exercises Home ↩

PREV :Tuple to Array Conversion.
NEXT : DataFrame to Array Conversion.

Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.