Skip to content
  • YouTube
  • FaceBook
  • Twitter
  • Instagram

Data Analytics Ireland

Data Analytics and Video Tutorials

  • Home
  • About Us
    • Latest
    • Write for us
    • Learn more information about our website
  • Useful Links
  • Glossary
  • All Categories
  • Faq
  • Livestream
  • Toggle search form
  • What are bitwise operators in Julia? julia
  • Recursion Definition
  • how to select all records with SQL SQL
  • ValueError: pattern contains no capture groups Value Error
  • Python Tutorial: How to sort lists Python Lists
  • How to pass multiple lists to a function and compare Python Functions
  • YouTube channel lists – Python working with files Python working with files
  • What is Javascript? Javascript

What Is An Array In Python?

Posted on January 18, 2022February 26, 2023 By admin

Estimated reading time: 3 minutes

Python arrays are used to manage how data is analyzed and have the data in a structured form for the data analyst or data scientist to use.

What is an Array?

An array has the following properties:

  1. The data in it is of the same data type.
  2. The data is stored as contiguous memory locations, meaning the lowest value is at index 0 and the last value is at the highest index value.
  3. Arrays assign index values to each data point contained within it.
  4. You can append values to the array.
  5. You can delete values from the array, that is it is mutable.

What are the differences between arrays and lists?

For the most part arrays and lists are the same, but with one difference:

A list can store any data type you want e.g. strings, integers, etc.

On the other hand, arrays can only store data that is of the same data type.

What are the different ways that I can create an array?

(A) Use Numpy

# Use numpy

import numpy as np

a = np.array([1,2,3,4])

print(a)
print(type(a))
print(a.dtype)

Output:
[1 2 3 4]
<class 'numpy.ndarray'>
int32

If we have one of the values as a string, all the other values are converted to a string , as follows:
# Use numpy

import numpy as np

a = np.array([1,2,3,'4'])

print(a)
print(type(a))
print(a.dtype)

Output:

['1' '2' '3' '4']
<class 'numpy.ndarray'>
<U11

# <U11 - When this happens, it means one of the values was returning as a string
# All the other values in the array as a result are converted to strings

(B) Use an array

import array as test_array

a = test_array.array('i',[1,2,3])

print(a)
print(type(a))

Output:
array('i', [1, 2, 3])
<class 'array.array'>

On the Python.org website, below is the list of values that can be populated into the above program, depending on what your need is:

When should I use arrays?

It really depends on the nature of your python program, but below are some examples that may help you make a decision:

(A) Many variables of the same type: There may be a scenario where you have to create an array to store data that is of the same data type. For example, you have a list of codes to look up against, which are all integers.

(B) Faster and more efficient: If speed is what you are looking for using arrays, will help improve the performance of your computer program, using lists is much slower.

(C) Compactability and efficiency: If the nature of your program needs to store large amounts of data that needs to be accessed quickly, then this would be a good reason to use them.

(D) Ability to retrieve data quickly through indexing: As arrays have index values associated with them, the data can be easily retrieved.

(E) Need to compute some mathematical values: Arrays are excellent for any numerical operations you need to complete, as the level of coding is minimal.

import array as test_array

a = test_array.array('i',[1,2,3])

mydivider = 2

mynewlist = [x / mydivider for x in a]
print(mynewlist)

result:
[0.5, 1.0, 1.5]

So in summary:

Speed, efficiency, and ease of use are the main reasons to use an array.

We use arrays here how to show percentage differences between files in python why not go over and see it in action!

array, numpy, Python, Python Tutorial Tags:array, Data Science, Python, python array, Python List, python numpy

Post navigation

Previous Post: How to show percentage differences between files in Python
Next Post: How to run Python directly from Javascript

Related Posts

  • Python Tutorial: How to validate data using tuples Python Tuples
  • Tkinter python tutorial Python
  • How to create a class in Python class
  • How To Validate Cell Values In Excel Python
  • how do I merge two dictionaries in Python? Python
  • How to remove characters from an imported CSV file Python Tutorial
  • How to use parameters in Python Python
  • How to Generate Random Integers Between 0 and 9 Python
  • how to select all records with SQL SQL
  • ValueError: Columns must be same length as key exception handling
  • python classes class
  • Tableau Desktop versus Tableau Server data visualisation
  • TypeError: ‘list’ object is not an iterator Python
  • Recursion Definition

Copyright © 2023 Data Analytics Ireland.

Powered by PressBook Premium theme

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT