TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to PYTHON

Python Array Examples

Use the array type to improve memory efficiency of numerical data.
Array. From a great height you view the earth. Consider a tree. It is not just a trunk, leaves, branches—it is atoms. Reality has great complexity. A good data type is needed.
Memory use. For efficiency, the array type is better than a list. It can hold ints. Code with arrays is more complex. But arrays make things that were previously impossible, possible.
An example. Here we start. Please add the "from import" statement at the top of the program. This lets you access the array type.

Constructor: The array constructor receives 2 arguments here: the first is a code. The code "i" here signifies an integer array.

And: In this example the second argument to array() is a list. The array will be made of integers equal to those specified in the list.

Finally: We use a for-loop to enumerate, and print, the elements in the array. This is the same syntax used for a list.

Python program that creates int array from array import array # Create an int array of three elements. a = array("i", [10, 20, 30]) # Display elements in array. for value in a: print(value) Output 10 20 30
String. Suppose you have a large string you want to store in an array. We can use the "u" type code. Here the string "python" is transformed into an array of six Unicode characters.

Tolist: We convert the array into a list with the tolist() method. We then join with an empty delimiter.

And: This yields a string. This approach can convert an array (or list) into a usable string.

join
Python program that creates char array from array import array # Create a Unicode char array. a = array("u", "python") # Display letters in array. for letter in a: print(letter) # Convert array to a list. # ... Then join it. s = "".join(a.tolist()) print(s) Output p y t h o n python
Append, insert. Many helpful methods are available on arrays. For example, the append method adds an element at the end of the array. We can also insert, remove and count.

Note: We create an empty int array in the first part. The second argument to the array init method is optional.

Python program that uses append, insert, remove, count from array import array # New int array. a = array("i") # Append three integers. a.append(100) a.append(200) a.append(300) # Insert an integer at index 1. a.insert(1, 900) # Remove this element. a.remove(200) # Count elements with this value. a.count(900) # Print. print(a) Output array('i', [100, 900, 300])
Memory. I tested 2 programs. The first version uses an array of 10 million integers. And the second uses a list of those same integers. I measured the memory before the program exited.

Result: The array with 10 million integers required 43.8 MB of memory. The list version required 710.9 MB.

Per element: For 10 million ints, the array used about 43 MB—about 4 bytes per integer. The list required closer to 70 bytes per element.

Caution: These are just estimates. Many factors, including garbage collection overhead, are in play.

And: One megabyte is not exactly 1 million bytes. It is 1,048,576 bytes. So the math is lacking.

Python program that uses int array from array import array # Create an empty int array. a = array("i") # Append ten million ints. for i in range(0, 10000000): a.append(i) # Finished. print("DONE") input() Python program that uses int list # Create an empty list. a = list() # Append ints. for i in range(0, 10000000): a.append(i) # Finished. print("DONE") input() Memory usage 43.8 MB Array 710.9 MB List
Benchmark, array. Is a small array faster than an equivalent list? Should we optimize programs by using more arrays? Looping over small arrays is slower than looping over lists.

Version 1: This version of the code uses a for-loop over an array. We sum each value in the array.

Version 2: Here we loop over a list. We print the time required for both versions of the code to complete.

Result: Arrays are slower than lists for small collection sizes. Consider arrays only when large amounts of data is being stored.

Python program that uses int array, list import time from array import array # Create an int array, and a list. list = list(range(0, 50)) arr = array("i", list) print(time.time()) # Version 1: loop over array. for i in range(0, 1000000): sum = 0 for value in arr: sum += value print(time.time()) # Version 2: loop over list. for i in range(0, 1000000): sum = 0 for value in list: sum += value print(time.time()) Output 1408894795.864125 [Python] 1408894803.765587 Array loop, 7.90 s 1408894811.303026 List loop, 7.53 s 1408894907.587 [PyPy3] 1408894908.066 Array loop, 0.48 s 1408894908.534 List loop, 0.47 s
Speed, overview. Using an array instead of a list in a numerical program could make the program many times faster. It could make a program that was previously unusable, usable.
Codes, bytes. There are many type codes available on arrays. Please consult the official Python documentation for a descriptive table. The number of bytes for each element type is shown.

Tip: A good developer should reference all helpful websites, not just ones with pictures.

Quote: Arrays are sequence types and behave very much like lists, except that the type of objects stored in them is constrained.

Efficient arrays of numeric values: Python.org
2D arrays. For an integer array, we can use a flattened array from the array module. There is no syntax support for a 2D array directly, but we can use special indexes to create one.2D Array
A summary. Arrays are essential in programs that compute large numerical data sets. We witnessed the memory improvement of an array over a list.
We used array methods, which are similar (but not identical) to lists. The types are complementary. For smaller data sets, lists are sufficient. And lists are friendlier in most ways.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf