Python List

Python List is a collection of ordered and changeable it allows duplicate members. The list is like a container used to store multiple data in same line. In list of python is used to store collection of object types. The next two types are dictionaries and tuple. Python list do work of most of collection of the data structures and don’t worry of manually creating them. Lists are like the arrays, and are declared in other languages. They are not homogeneous always this makes it most powerful tool in Python. The single list may contain Data types are like Integers, objects, as well as strings it   used for the implementation of stacks and queues The list in Python has fixed count and has an order.

The elements in a list are indexed according to a sequence and indexing of a list is done with 0 the first index.

Each element in the list has its definite place in the list it allows duplicating of elements in the list.

Lists are a useful tool for preserving a sequence of data and further iterating over it.
In python list is created by placing elements inside a square bracket [], separated by commas.
List in python are used to store collection of items.
You can recognize list by their square bracket [and] that hold.
Python list are the c array inside python interpreter and act like array of pointers.

Creating list:-


It does not need a build in function for creation of list.
List may contain duplicate values with distinct positions.
Multiple duplicate and distinct values are passed as in sequence of time of creation of list.
List is created by placing all the elements in sequence inside the square bracket [], and is separated by commas.
e.g of sample list:-
thislist = ["mango", "apple", "cherry"]
            print(thislist)
O/p:-
[‘mango’,’apple’,’cherry’]
In this there are no of items like int, float, and string as shown in below,

Example of creating an empty list:-


           The below list shows that the list is empty.
my_list=[]

Example of defining the list:-


my_list=[1,2,3,4]
my_list=[1,”Hello python”,3,4]
List is used in almost every program in python.
Defining the list in python is very easy task.
You need to provide name of list and initialize with value.
e.g:-
mylist=[“python”,”is”,”verry”,”simple”,”language”]
mylist
[‘Python’,’is’,’verry’,’simple’,’language’]
You see that a list named ‘myList’ is created with some elements.
Lists in python are zero indexed.
This means, you can access individual elements in a list as you would do in an array.

Accessing the list in python:-


You access the list items by referring the index number
There are two types of accessing list:-

Positive Indexing

Negative Indexing

Positive Indexing:-

e.g:-
thislist = ["mango", "apple", "cherry"]
print(thislist[2])
O/p:-
cherry

Negative Indexing:-

The negative indexing means beginning from last /end of the list i.e. -1 refers to last item then -2 the second last item, etc.

e.g:-
thislist = ["mango", "apple", "cherry"]
print(thislist[-1])
O/p:-
cherry

Range of Indexes:-

You specify a range of indexes by specify where to start and where to end the range.
When specifying a range, the return value will be in the new list with the specified items.
e.g:-
thislist = ["Banana", "mango", "apple", "orange", "kiwi", "melon", "mango"]
print(thislist[1:5])
O/p:-
[‘mango’,‘apple’,’orange’,’kiwi’]
#This will return the items from position 1 to 5.
#Remember that the first item is position 0,
#and note that the item in position 5 is NOT included

Range of Negative Indexes:-


In the negative indexes you want to start search from end of list.
e.g:-
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[-2:-1])
O/p:-
[‘Melon’]Explanation of the above example  is given:-
Negative indexing means starting from the end of the list.
This example returns the items from index -2 (included) to index -1 (excluded)
Remember that the last item has the index -1.

Change Value of the item in list:-

e.g:-
thislist = ["apple", "kaju", "cherry"]
thislist[1] = "banana"
print(thislist)
O/p:-
[‘mango’,’cherry’,’banana’]
The explanation is adding new item by replacing the original one is done here.

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India