Accessing Array Items in Python: Retrieving Elements from Arrays

thumb_up 1  ·  sell Python array item access, Accessing elements in Python arrays, Retrieving array items in Python

Since the array object behaves very much like a sequence, you can perform indexing and slicing operation with it.

Example

 
import array as arr a = arr.array('i', [1, 2, 3]) #indexing print (a[1]) #slicing print (a[1:])

Changing Array Items

You can assign value to an item in the array just as you assign a value to item in a list.

Example

 
import array as arr a = arr.array('i', [1, 2, 3]) a[1] = 20 print (a[1])

Here, you will get "20" as the output. However, Python doesn't allow assigning value of any other type than the typecode used at the time of creating an array. The following assignment raises TypeError.

 
import array as arr a = arr.array('i', [1, 2, 3]) # assignment a[1] = 'A'

It will produce the following output −

TypeError: 'str' object cannot be interpreted as an integer



The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back