Python List or Array

Posted in /   /  

Python List or Array
vinaykhatri

Vinay Khatri
Last updated on March 28, 2024

    Mostly all 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 data types, such as integers, float, etc.

    But in the array, Python does not support the in-built Array data type. Instead of an array, Python has lists, dictionaries, tuples, and sets. All these work as an array, but not an array.

    Having datatypes that look and work like arrays in Python was inefficient, as Python has a 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 was introduced in Python, but it was not declared as the built-in data type.

    If you have heard the arrays in Python , it’s not called the Python array; it’s the NumPy Array. Yet Python does not have the built-in type called an 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 that makes it possible to use the concept of Array in Python. The NumPy is used to serve the scientific calculation, which the Python list cannot solve. NumPy is written in Python and C.

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

    The answer is as simple as the calculation in Python List becomes complicated and does not give a satisfactory answer when it comes to 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 does have an object which looks and works like an array. In an array, we use the index to call a particular value. So, in the list, dictionaries, tuples, and sets, we, too, use the index to call a specific value.

    list() - Lists are one of the objects in Python. The list looks like arrays that store multiple values. The main difference is that list can store non-homogeneous values in one variable, which means a single list variable can store integers, float, Strings, and other objects. A list can be created using the square bracket. As with an array, the indexing of a 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 with 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 with 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:

    A dictionary is a collection of key and value pairs. It stores unordered items. Every value in the dictionary has a corresponding key.

    Dictionaries use curly braces to store the keys and values. The dictionary's values can be mutable, meaning the value can be changed after assigning it. But, the keys associated with the values are immutable, and you cannot change them 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 lists are mutable. The immutability in tuples is once the value is assigned to an index. After that, you cannot grab the index to change the value if you try, it will show an error. Tuples use parenthesis to store 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; the only difference is you cannot assign two same values. If you try to do so, it will treat it as one and 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 Comment on this Post

    0 Comments