❮ Previous Next ❯

Python Lists

As the name suggests, the List is the collection of homogeneous or heterogeneous types of values. For example we have a list and the variable name for the list is names, like


here list is identified by the square bracket symbol, and the [](square bracket) will contain the collection of values.
A list have zero values or maximum of values like 100 values, 1000 values etc.

For example;
>>> names=[]
>>> names[]

python lists

Here this is an empty list that we got when we call names .
Now we can initialize list, that means we can assign values to list.

For example,
>>> names=["Sashi","John","Alex"]
>>> names
['Sashi', 'John', 'Alex']          # output when we call names

Now this list having 3 values.
Now there is a term index in python list that represents the position of the value in list. Here position of list starting from 0 then 1, then 2, and so on.

For example:
>>> names[0]
>>> names[1]
>>> names[2]

Now there is an interesting thing that you can have backward index also, that means we can traverse a list from backward direction. In backward direction index is starting from -1, then -2, then -3, and so on.

For example:
>>> names[-1]
>>> names[-2]
>>> names[-3]
Here in our context, the forward index starts from 0 then 1 then 2, and backward index starts from -1 then -2 then -3.

Now I want to add a new data item, then we call append() : an in-built function in Python, like

>>> names.append("Sanghamitra")
>>> names
['Sashi', 'John', 'Alex', 'Sanghamitra']

Let we have another list and the variable name for the list is age, like

>>> age=[20,25,30,35]

Now we want to pass list age in list names, for this we call extend() function

>>> age=[20,25,30,35]
>>> names.extend(age)
>>> names
['Sashi', 'John', 'Alex', 'Sanghamitra', 20, 25, 30, 35]

Now we want to remove an item from a list, then we call remove() function.

For example:
>>> names.remove("John")
>>> names
['Sashi', 'Alex', 'Sanghamitra', 20, 25, 30, 35]
Now we want to print multiple lists
>>> print(names)
['Sashi', 'Alex', 'Sanghamitra', 20, 25, 30, 35]
>>> print(names,age)
['Sashi', 'Alex', 'Sanghamitra', 20, 25, 30, 35] [20, 25, 30, 35]

To find the length of list, we call len() function. The number of data items present in a list is the length of list.

For example:
>>> len(names)
To get the maximum and minimum value, we call max() and min()function.
>>> max(age)
>>> min(age)

❮ Previous Next ❯

Follow Us


Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Email: info@silantechnology.com
      Phone: 0674-2361252
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved