๐งฎ Python Arrays โ Creation, Manipulation, and Operations
๐งฒ Introduction โ Why Use Arrays in Python?
In Python, an array is a data structure that stores multiple items of the same type in a single variable. Arrays offer efficient memory usage and fast indexing, making them useful when working with large collections of numerical data.
Although Python lists are more commonly used, the array
module is better suited for performance-sensitive applications that require homogenous data types.
๐ฏ In this guide, you’ll learn:
- How to create arrays and perform basic operations
- Methods for accessing, adding, removing, and looping through items
- Advanced tasks like copying, reversing, sorting, and joining arrays
- Real-world practice exercises to reinforce concepts
๐ Topics Covered
๐งฉ Topic | ๐ Description |
---|---|
Python Arrays | Introduction to arrays and their syntax |
Access / Add / Remove / Loop Items | Working with elements using indexes and loops |
Copy / Reverse / Sort / Join Arrays | Array manipulation techniques |
Python Array Methods | Built-in methods like append() , remove() , pop() , etc. |
Python Array Exercises | Practice examples to reinforce key concepts |
๐ ๏ธ Python Arrays โ Getting Started
Python’s array
module provides array support:
import array
arr = array.array('i', [1, 2, 3, 4, 5])
print(arr)
โ
'i'
stands for integer type. Other type codes: 'f'
(float), 'd'
(double), 'u'
(Unicode char), etc.
๐ Accessing, Adding, Removing, Looping Array Items
๐น Access Elements
print(arr[0]) # 1
print(arr[-1]) # 5
๐น Add Items
arr.append(6)
arr.insert(2, 10)
๐น Remove Items
arr.remove(3)
arr.pop() # Removes last element
๐น Loop Through Array
for num in arr:
print(num)
๐ Copy, Reverse, Sort, and Join Arrays
๐น Copy Array
arr2 = arr[:]
๐น Reverse Array
arr.reverse()
๐น Sort Array
sorted_arr = sorted(arr)
๐น Join Arrays
arr3 = array.array('i', [7, 8, 9])
combined = arr + arr3
๐ Python Array Methods
Method | Description |
---|---|
append(x) | Add item x at the end |
insert(i, x) | Insert x at index i |
remove(x) | Remove first occurrence of x |
pop() | Remove and return last item |
reverse() | Reverse the order of elements |
extend(iter) | Append items from iterable |
index(x) | Return index of first occurrence of x |
count(x) | Count occurrences of x |
๐งช Python Array Exercises
โ 1. Create an Array of Integers
import array
nums = array.array('i', [10, 20, 30])
print(nums)
โ 2. Append and Insert Items
nums.append(40)
nums.insert(1, 25)
print(nums)
โ 3. Remove and Pop Items
nums.remove(25)
nums.pop()
โ 4. Reverse and Copy an Array
reversed_nums = nums[:]
reversed_nums.reverse()
print(reversed_nums)
โ 5. Join Two Arrays
more_nums = array.array('i', [50, 60])
combined = nums + more_nums
print(combined)
๐ Summary โ Recap & Next Steps
Arrays in Python provide efficient ways to store and manipulate homogenous data. While lists are more flexible, arrays offer better performance in numeric-heavy tasks.
๐ Key Takeaways:
- Use
array.array(typecode, iterable)
to create arrays. - Perform element operations with
append()
,insert()
,remove()
, and loops. - Use slicing and built-in methods to reverse, copy, and extend arrays.
โ๏ธ Real-World Relevance:
Arrays are crucial in numerical computations, simulations, memory-optimized systems, and data science applications.
โ FAQ โ Python Arrays
โ Whatโs the difference between a list and an array in Python?
โ
Lists can store mixed types, but arrays (from the array
module) store only one data type and use less memory.
โ When should I use Python arrays over lists?
โ
Use arrays when working with large sets of numbers and performance is critical.
โ Can I sort an array directly?
โ
You can use sorted(arr)
for a new array or manually implement sorting if using array
.
โ How do I create an empty array?
import array
empty = array.array('i')
โ Can I use slicing with arrays like lists?
โ
Yes. Arrays support slicing: arr[1:3]
gives a sub-array.
Share Now :