Thursday, May 16, 2019

Python List is more than just a List

Python is the most popular programming language today, We can understand the growth of Python by it's continuously growing ratio of it's related fields like Data Science, Python Developer, Deep Learning Researcher and many more fields which are related to Python.
So for Now, if you want to engage your career with these technologies, It the first prerequisite that you should know little bit about Python.

So lets talk about today's topic that is Python List. So if you don't know anything about Python List then I will recommend you to check the first tutorial on list
So for Now, I am going to tell you a little bit more about Python List.

Python List is more than just a List :

Python 's List can make your Data Structure easy to implement, So let's try to take a deep dip inside this Python List, when we use a Python data structure that holds many Python objects, It can holds as many object, that's by it's called list, We can create a list of integers as follows.

In[1]: L = list(range(10))
L
Out[1]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In[2]: type(L[0])
Out[2]: int

Or, similarly, a list of strings: Which helps you to build a strong basic concept of Python List.

In[3]: L2 = [str(c) for c in L]
L2
Out[3]: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
In[4]: type(L2[0])
Out[4]: str

Because of the Python’s dynamic typing, we can even create Heterogeneous Lists also. Let's see the example of Heterogeneous List.

In[5]: L3 = [True, "2", 3.0, 4]
[type(item) for item in L3]
Out[5]: [bool, str, float, int]


But this flexibility comes at a cost to allow these flexible types, each item in the list must contain its own type info, reference count, and other information—that is, each item is a complete Python object.

Again, the advantage of the list is flexibility: 
because each list element is a full structure containing both data and type information, the list can be filled with data of any desired type.

The difference between a dynamic-type list and a fixed-type (NumPy-style) array is illustrated in the given image below.
Creating an Array's from Python List :
           
             First, we can use np.array to create arrays from Python lists.                                         
In[1]: import numpy as np
In[2]: # integer array:
np.array([1, 4, 2, 5, 3])
Out[2]: array([1, 4, 2, 5, 3])

Remember that : 
Unlike Python lists, NumPy is constrained to arrays that all contain the same type. If types do not match, NumPy will upcast if possible (here, integers are upcast to floating point).

In[1]: np.array([3.14, 4, 2, 3])
Out[1]: array([ 3.14, 4. , 2. , 3. ])

We can also Set the data type of our resulting Array at the initialize time using dtype Keyword.

In[9]: np.array([1, 2, 3, 4], dtype='float32')
Out[9]: array([ 1., 2., 3., 4.], dtype=float32

Here is the  one way of initializing a multidimensional array using a list of lists :

In[7]: # nested lists result in multidimensional arrays
np.array([range(i, i + 3) for i in [2, 4, 6]])
Out[7]: array([[2, 3, 4],
[4, 5, 6],
[6, 7, 8]])

At Last Thanks for Reading...
, , , , , ,

2 comments:


  1. I never thought about these thing which we can do using Python List. Awesome.

    ReplyDelete
    Replies
    1. I also did not thought these things Bro. Same Here.

      Delete