Skip to content
  • YouTube
  • FaceBook
  • Twitter
  • Instagram

Data Analytics Ireland

Data Analytics and Video Tutorials

  • Home
  • Contact
  • About Us
    • Latest
    • Write for us
    • Learn more information about our website
  • Useful Links
  • Glossary
  • All Categories
  • Faq
  • Livestream
  • Toggle search form
  • How to save data frame changes to a file Python Dataframe
  • select rows with a certain value using SQL SQL
  • How to data cleanse a database table Python Data Cleansing
  • Regular expressions python Python
  • raise an exception in python class
  • Python tutorial: Pandas groupby ( Video 1) Python
  • TypeError: ‘int’ object is not callable Python
  • How to Pass a Javascript Variable to Python using JSON Flask

What Is An Array In Python?

Posted on January 18, 2022July 11, 2022 By admin No Comments on What Is An Array In Python?

Estimated reading time: 3 minutes

Python arrays are used to manage how data is analysed 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 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 are 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 maybe 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 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, they data can be easiy 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 in 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

  • How To Pass Data Between Functions Python Functions
  • How to Add Formulas to Excel using Python numpy
  • Python tutorial: Create an input box in Tkinter Python
  • How to Pass a Javascript Variable to Python using JSON Flask
  • planning your machine learning model machine learning
  • how to create charts in Tkinter Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Select your language!

  • हिंदी
  • Español
  • Português
  • Français
  • Italiano
  • how to copy/paste special a range of cells with xlwings Python
  • Deleting table records with SQL SQL
  • How to Pass a Javascript Variable to Python using JSON Flask
  • R tutorial – How to sort lists using rstudio R Programming
  • python constructor self and __init__explained Python
  • TypeError: ‘float’ object is not callable Python
  • How to remove characters from an imported CSV file Python Tutorial
  • how to update records in SQL CRUD

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