How to add elements to one-dimensional array in python

Release: 2019-07-08 09:11:15
Original
17161 people have browsed it

How to add elements to one-dimensional array in python

An array is an ordered collection in which elements can be added and removed at any time. You can use the append() function to add new objects to the end of the array. You can also use the insert() function to insert a specified object into a specified position in the array.

1. append() function:

append() function syntax:

list.append(obj)
Copy after login

Parameters: obj -- the object added to the end of the list.

Return value: This method has no return value, but it will modify the original list.

The following example shows how to use the append() function:

aList = [123, 'xyz', 'zara', 'abc'];
aList.append( 2009 );
print "Updated List : ", aList;
Copy after login

The output results are as follows:

Updated List :  [123, 'xyz', 'zara', 'abc', 2009]
Copy after login

2. insert() function:

Syntax:

list.insert(index, obj)
Copy after login

Parameters:

index -- The index position where object obj needs to be inserted. obj – the object to be inserted into the list.

Return value: This method has no return value, but will insert the object at the specified position in the list.

The following example shows how to use the insert() function:

aList = [123, 'xyz', 'zara', 'abc'] 
aList.insert( 3, 2009) 
print "Final List : ", aList
Copy after login

Output result:

Final List : [123, 'xyz', 'zara', 2009, 'abc']
Copy after login

For more Python related technical articles, please visit Python Tutorial column for learning!

The above is the detailed content of How to add elements to one-dimensional array in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template