Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python Projects - Beginner to Advanced
Next article icon

The unpacking techniques you use with lists also apply topython 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 inPython, 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
importarrayarr=array.array('i',[10,20,30])a,b,c=arrprint(a)print(b)print(c)

Output
102030

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
importarrayarr=array.array('i',[1,2,3,4,5])a,*rest,e=arrprint(a)# 1print(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
importarrayarr1=array.array('i',[1,2])arr2=array.array('i',[3,4])arr3=[arr1,arr2](a,b),(c,d)=arr3print(a,b)# 1 2print(c,d)# 3 4

Output
1 23 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
importarrayarr=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.

Improve
Practice Tags :

Similar Reads

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 ourCookie 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

[8]ページ先頭

©2009-2025 Movatter.jp