Python Access Array Item Last Updated : 08 Dec, 2024 Comments Improve Suggest changes Like Article Like Report In Python, arrays can be used as an alternative to lists when we need more efficient storage of data. In this article, we will explore how to access array items using the array module in Python.Once array is created, we can access its items just like accessing elements in a list. Here are some ways to access array items:Accessing Elements by IndexArrays in Python are zero-indexed. The first element is at index 0, the second at index 1 and so on. You can access elements of an array using their index: Python import array # Creating an array of integers arr = array.array('i', [10, 20, 30, 40, 50]) # Accessing elements by index print(arr[0]) print(arr[1]) print(arr[2]) Let's explore other methods of accessing elements by index in python:Table of ContentAccessing Elements from the EndSlicing ArraysIterating Through Array ItemsUsing index() to Find an ItemModifying Array ItemsChecking If an Item Exists in the ArrayAccessing Elements from EndWe can also access elements from the end of the array using negative indices. A negative index counts from the last element, where -1 represents the last element, -2 represents the second-to-last and so on: Python import array # Creating an array of integers arr = array.array('i', [10, 20, 30, 40, 50]) # Accessing elements from the end print(arr[-1]) # (last element) print(arr[-2]) # (second to last element) Slicing Arrays to Get SubsetJust like lists, we can slice arrays to access a subset of elements. The slice syntax allows us to specify a start index, an end index and an optional step: Python import array # Creating an array of integers arr = array.array('i', [10, 20, 30, 40, 50]) # Slicing the array print(arr[1:4]) Iterating Through Array ItemsWe can also iterate over the elements of an array using a loop. This is useful when we want to perform some operation on each item: Python import array # Creating an array of integers arr = array.array('i', [10, 20, 30, 40, 50]) # Iterating through the array for i in arr: print(i) Using index() to Find an ItemIf we need to find the index of a specific item in the array, we can use the index() method. This method returns the index of the first occurrence of the item: Python import array # Creating an array of integers arr = array.array('i', [10, 20, 30, 40, 50]) # Finding the index of an item idx = arr.index(30) print(idx) # (index of 30) Modifying Array ItemsWe can modify the value of an element in an array by accessing it via its index and assigning a new value: Python import array # Creating an array of integers arr = array.array('i', [10, 20, 30, 40, 50]) # Modifying an element arr[2] = 35 print(arr) Checking If an Item Exists in the ArrayWe can use the in operator to check if an item exists in an array. This will return True if the item is found and False if it's not: Python import array # Creating an array of integers arr = array.array('i', [10, 20, 30, 40, 50]) # Checking if an item exists in the array print(30 in arr) print(60 in arr) Comment More infoAdvertise with us Next Article Python Access Array Item A anuragtriarna Follow Improve Article Tags : Python Python Programs Python-array Practice Tags : python Similar Reads Python - Access List Item Whether we are working with numbers, strings or other data types, lists provide a versatile way to organize and manipulate data. But how to access specific items in a list? This article will guide you through various methods of accessing list items in Python.Accessing List Items by IndexIn Python, l 3 min read Python Access Set Items Python sets are unordered collections of unique elements. Unlike lists or dictionaries, sets do not have indices so accessing elements in the traditional way using an index doesn't apply. However, there are still ways to work with sets and access their items effectively. This article will guide you 2 min read Python Access Tuple Item In Python, a tuple is a collection of ordered, immutable elements. Accessing the items in a tuple is easy and in this article, we'll explore how to access tuple elements in python using different methods.Access Tuple Items by IndexJust like lists, tuples are indexed in Python. The indexing starts fr 2 min read Find element in Array - Python Finding an item in an array in Python can be done using several different methods depending on the situation. Here are a few of the most common ways to find an item in a Python array.Using the in Operatorin operator is one of the most straightforward ways to check if an item exists in an array. It r 3 min read Python - Add List Items Python lists are dynamic, which means we can add items to them anytime. In this guide, we'll look at some common ways to add single or multiple items to a list using built-in methods and operators with simple examples:Add a Single Item Using append()append() method adds one item to the end of the li 3 min read Python Unpack Array 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 UnpackingTo begin 3 min read Extract Elements from a Python List When working with lists in Python, we often need to extract specific elements. The easiest way to extract an element from a list is by using its index. Python uses zero-based indexing, meaning the first element is at index 0. Pythonx = [10, 20, 30, 40, 50] # Extracts the last element a = x[0] print( 2 min read Access the Index and Value using Python 'For' Loop In this article, we will get to know how to access an index value in a for loop in Python. There are many ways to access an index value in a for loop in Python but we will be studying mainly four of them using a for loop. Python programming language supports the different types of loops, the loops c 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 How to Create String Array in Python ? To create a string array in Python, different methods can be used based on the requirement. A list can store multiple strings easily, NumPy arrays offer more features for large-scale data and the array module provides type-restricted storage. Each method helps in managing collections of text values 2 min read Like