Python List or Array

By | February 4, 2019

Mostly all the Programming languages use the concept of Array, but in the case of Python, it doesn’t. Python is one of the most powerful and popular languages out there. It has many datatypes such as Integers, Float, List dictionaries etc. but when it comes to the concept of array Python does not support the concept of the in-built Array data type. Instead of array Python has lists, Dictionaries, tuples and sets all these work as an array, but not an array.

Having datatypes which look and works like arrays in Python were not efficient, as python has the versatile nature and missing the concept of an array could have been a bad deal for a Python developer, so to overcome this constraint the concept of Array introduced in Python but it was not declared as the build-in data type. If you have ever heard the arrays in Python, it’s not called as the Python array; actually it’s the NumPy Array. Yet Python does not have the built-in type called array but if you want to use the array in python, you need to download NumPy.

What is NumPy?

NumPy is an Open source Python core library which makes possible to use the concept of Array in Python. The NumPy is used to serve the scientific calculation which cannot be solved by the Python list. NumPy is written in Python and C.

Why do we need NumPy array even though we have Python List that looks like Array?

The answer as simple as that the calculation in Python list become complicated and does not give the satisfactory answer when it comes to the scientific calculation. The calculation speed of the NumPy Array is faster than the Python List.

Main Features of NumPy

  • Numerical Analysis
  • Linear Algebra.
  • Matrix Computation.

Syntax of NumPy Array in Python

from NumPy import*

Arr =[[2,3,4],[6,7,8],[9,10,11]]

Arr1 = array(Arr) #here we have declared Arr1 as an array

Python Objects like Arrays

Python do have object which looks and works like array. In array, to call a particular value we use index, so do in list, dictionaries, tuples and sets we too use the index to call a specific value.

list()

lists are one of the object in Python. List looks like arrays which stores multiple values. The main difference is that list can store non-homogeneous values in one variable that mean a single list variable can store integers, float, Strings, and another object.

A list can be created using the square bracket. As like array, the indexing of list starts with 0. In the list, the index can also be a –ve value.

# Syntax to declare a Python list

Lis = [“hello”, “world”, 1, 2, 3]

print(Lis[0])

print(Lis[1])

print(Lis[2])

print(Lis[3])

print(Lis[-1]) #Here we have used the –ve index call.

# Output will be

hello

world

1

2

3

As the list is a Python object so there are many built in methods or functions associated to it.

The methods related to the lists are

  • append()
  • extend()
  • insert()
  • remove()
  • pop()
  • clear()
  • index()
  • count()
  • sort()
  • reverse()
  • copy()

#Syntax to access the method of list

lis=[1,2,3]

lis.append(4)

print(lis)

#output

[1,2,3,4]

The functions Associated to the list:

  • round()
  • reduce()
  • sum()
  • ord()
  • cmp()
  • max()
  • min()
  • all()
  • any()
  • len()
  • enumerate()
  • accumulate()
  • filter()
  • map()
  • lambda()

#Syntax to access a list function

lis=[1,2,3]

print(max(lis))

#output

3

Python Dictionaries:

Dictionary is a collection of keys and values pairs. Dictionary store the unordered items. Every value in the dictionary has a corresponding key. Dictionaries use curly braces to store the keys and values.

The values of the dictionary can be mutable that means the value can be changed after it assigned but in the keys associated to the values are immutable that doesn’t allow to change once assigned.

#Syntax to assign a Dictionary

Dic={“m”: “man”, “w”: “woman”, “c”: “child”,1:”Age”} # Keys:Values

print(Dic[“m”])

print(Dic[1])

#output

man

Age

Dictionaries methods:

  • clear()
  • copy()
  • fromkeys(seq[, v])
  • get(key[,d])
  • items()
  • keys()
  • pop(key[,d])
  • popitem()
  • setdefault(key[,d])
  • update([other])
  • values()

tupels() in Python

‘tuples’ are very similar to lists, the only difference is, tuples are immutable whereas list are mutable. The immutability in tuples refers that once the value is assigned to an index, after that you cannot grab the index to change the value if you try to do so it will show an error.

Tuples use parenthesis to stores the value.

# Tuple Syntax

t = (1,2,3)

print(t[1])

print(t[-1])

#output

2

3

Methods related to the tuples

  • count()
  • index()

‘sets’ in Python

‘sets’ is the collection of unique elements in an unordered manner. Sets are similar to the list, only difference is you cannot assign 2 same values. If you try to do so it will treat it as one and do not show the repetitive value.

#Syntax of sets

Ms =[1,2,3,3,4,5]

print(set(Ms))

#output will be:

{1,2,3}

Leave a Reply

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