0% found this document useful (0 votes)
8 views3 pages

List As Array

An array is a collection of similar elements that can be accessed by index, starting from 0, and can be handled in Python using the array module. The document provides syntax for creating an array, examples of summing array elements, and converting a list into an array using the fromlist() function. It also includes sample code demonstrating these functionalities.

Uploaded by

r0761808
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

List As Array

An array is a collection of similar elements that can be accessed by index, starting from 0, and can be handled in Python using the array module. The document provides syntax for creating an array, examples of summing array elements, and converting a list into an array using the fromlist() function. It also includes sample code demonstrating these functionalities.

Uploaded by

r0761808
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

List as array:

Array:

Array is a collection of similar elements. Elements in the array can be accessed


by index. Index starts with 0. Array can be handled in python by module
named array.
To create array have to import array module in the program.

Syntax :
import array

Syntax to create array:


Array_name = module_name.function_name(‘datatype’,[elements])

example:
a=array.array(‘i’,[1,2,3,4])
a- array name
array- module name
i- integer datatype

Example
Program to find sum of array elements
import array
sum=0
a=array.array('i',[1,2,3,4])
for i in a:
sum=sum+i
print(sum)
Output
10

Convert list into array:

fromlist() function is used to append list to array. Here the list is act like a
array.
Syntax:
arrayname.fromlist(list_name)

Example

Program to convert list into array


import array
sum=0
l=[6,7,8,9,5]
a=array.array('i',[])
a.fromlist(l)
for i in a:
sum=sum+i
print(sum)

Output
35
Methods in array
a=[2,3,4,5]

You might also like