Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python Unpack Array
Next article icon

Python Unpack Array

Last Updated : 06 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The unpacking techniques you use with lists also apply to python arrays, but with slight differences due to the nature of the array module. In this article, we'll explore how to unpack arrays using the array module in Python, and demonstrate various methods of unpacking.

Basic Array Unpacking

To begin with, let's see how we can unpack values from an array using the array module.

Python
import array  arr = array.array('i', [10, 20, 30]) a, b, c = arr print(a)   print(b)   print(c)   

Output
10 20 30 

Explanation:

  • Here, we create an array arr of type 'i', which means it stores integers. The values [10, 20, 30] are packed into this array.
  • We use the unpacking syntax a, b, c = arr to assign the array's values to the variables a, b, and c.
  • The array values are unpacked into the respective variables, similar to list unpacking.

Let's explore other methods unpacking an array in python:

Unpacking with Asterisk (*) Operator

Using the * operator for unpacking works similarly with arrays as it does with lists. This allows you to capture multiple values in the middle of an array.

Python
import array  arr = array.array('i', [1, 2, 3, 4, 5]) a, *rest, e = arr print(a)     # 1 print(rest)  # [2, 3, 4] print(e)     # 5 

Output
1 [2, 3, 4] 5 

Explanation:

  • In this example, the arr array contains the values [1, 2, 3, 4, 5].
  • a gets the first element (1), and e gets the last element (5).
  • The *rest captures the middle elements ([2, 3, 4]) into a list.
  • The use of *rest allows for flexible unpacking when you want to capture all but the first and last elements of the array.

Unpacking Nested Arrays

Arrays in Python can also contain other arrays, allowing you to work with nested structures. You can unpack nested arrays using a similar technique to unpacking simple arrays.

Python
import array  arr1 = array.array('i', [1, 2]) arr2 = array.array('i', [3, 4]) arr3 = [arr1, arr2]  (a, b), (c, d) = arr3 print(a, b)  # 1 2 print(c, d)  # 3 4 

Output
1 2 3 4 

Explanation:

  • Here, arr3 is a list containing two array objects: arr1 and arr2.
  • The unpacking syntax (a, b), (c, d) = arr3unpacks each array into its respective values.
  • The variables a, b, c, and d are assigned values from the two arrays, and the result is printed.

Unpacking Using Indexing and Slicing

You can also slice an array and unpack the results. This can be particularly useful if you want to extract only specific parts of an array.

Python
import array  arr = array.array('i', [10, 20, 30, 40, 50]) first_two, *rest = arr[:2], arr[2:] print(first_two)  # [10, 20] print(rest)       # [30, 40, 50] 

Output
array('i', [10, 20]) [array('i', [30, 40, 50])] 

Explanation:

  • We slice the array into two parts: arr[:2] (the first two elements) and arr[2:] (the remaining elements).
  • We then unpack the two slices into first_two and rest.
  • This technique allows for more control over what parts of the array you want to unpack.

Next Article
Python Unpack Array

A

anuragtriarna
Improve
Article Tags :
  • Python
  • Python Programs
  • Python-array
Practice Tags :
  • python

Similar Reads

    Python Unpack List
    Unpacking lists in Python is a feature that allows us to extract values from a list into variables or other data structures. This technique is useful for various situations, including assignments, function arguments and iteration. In this article, we’ll explore what is list unpacking and how to use
    3 min read
    Unpack Dictionary In Python
    Unpacking a dictionary in Python means extracting its values and assigning them to variables, often in a more structured or readable way. For example, given a dictionary d = {"name": "Bob", "age": 22, "major": "Computer Science"}, you can unpack its values into separate variables like a = "Bob", b =
    3 min read
    Unpacking Nested Tuples-Python
    The task of unpacking nested tuples in Python involves iterating through a list of tuples, extracting values from both the outer and inner tuples and restructuring them into a flattened format. For example, a = [(4, (5, 'Gfg')), (7, (8, 6))] becomes [(4, 5, 'Gfg'), (7, 8, 6)].Using list comprehensio
    3 min read
    Python | Unpacking tuple of lists
    Given a tuple of lists, write a Python program to unpack the elements of the lists that are packed inside the given tuple. Examples: Input : (['a', 'apple'], ['b', 'ball']) Output : ['a', 'apple', 'b', 'ball'] Input : ([1, 'sam', 75], [2, 'bob', 39], [3, 'Kate', 87]) Output : [1, 'sam', 75, 2, 'bob'
    3 min read
    Convert list to Python array - Python
    We can use a number of approaches to convert a list into a Python array based on the requirements. One option is to use the array module, which allows us to create arrays with a specified data type. Another option is to use the numpy.array() method, which provides more features for working with arra
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences