Linux

Python List

A Python list is a mutable sequence of data values called items or elements. An item can be of any type. Here are some real-world examples of lists:

Creating Python List

In this section you will learn how to create a Python List.

Empty List

 list1 = []
The empty Python list is written as two square brackets containing nothing.

Creating Python list with element

To create the list fill the values in list separated by commas with square brackets.
avengers = ['hulk', 'iron-man', 'loki', 'Captain', 'Thor']

Python List Operations

In a list you can access, update, add and delete elements of from the list

Accessing Values in Python Lists

In order to access list values use list name with index in square brackets. Following is a simple example.
Accessing value of the Python list
Accessing value of the list
you can access slice of a Python list by providing indices of the list.

Updating Python Lists

Because list are mutable. You can assign existing value new value. Below example showing using index we can change the value of list.
Update value of the Python list
Updating value of the list
The above figure shows that Captain is changed to Captain-America.

Delete Python List Elements

By using del you can delete the element or slicing of list.
Let us discuss in next example
deleting value of the Python list
deleting value of the list
You can give index for single element and indices for more than one element.

Addition of Python Lists

You can add two lists by using + operator as we have learned in tuples.
Addition of Python lists
Addition of lists

Multiplication of Python List

Similarly you can use multiplication operator * to multiply the list see the exmaple below
Multiplication of Python lists
Multiplication of list

in operator

Below is the example to use in operator,
Python in operator
in operator

Python list in for loop

Below is the example to use for loop,
For loop Python list
list with for loop
You can see we can use list with for loop.



Python List functions

Python includes the following list functions:

len()

Syntax
len(list)

it discussed in previous Python tuple, same here len() function gives the length of the list. Let us discuss a example.
length of Python list by len()
length of Python list

max()

Syntax
max(list)

The method max() returns the elements from the Python list with maximum value. Let us discuss a example.
max of Python list
max of list

min()

Syntax
min(list)

The method min() returns the elements from the Python list with minimum value. Let us discuss a example.
min of Python list
min of list

list()

Syntax
list(list1)

The method list() convert the a sequence such as Python tuple intoPython list. Let us discuss a example.
Conversion of Python tuple and string into Python list
list conversion to tuple
You can see the tuple tuple1 and string str1 converted into list.

cmp()

Syntax
cmp(list1,list2)

The method cmp() compares elements of two Python lists . Let us discuss a example.
Python list comparison
list comparison
It compare the first list list1 to second list list2. It simply first compare the 1 value. If fisrt value of list1 is lesser than the list2 then it will print -1.

Python List methods

Python includes the following list .

append()

Syntax
list.append()

This method add an value at the end of the existing list.
Python list append()
list append
You can see avengers list contain only one member, by using append() method we have add Hulk and Thor .

extend()

Syntax
list1.extend(givenlist)

list1 the primary list, which will be extended.
givenlist The list which will be add to list1 The extend() extends the Python list by appending all the items in the given list. Below is example of famous Avenger team
Python list extend()
list extend
You can see avengers army contain more members.


Difference between append() and extend()
The following example will clear remaining doubt
difference between append() and extend()
difference b/w append() and extend()
The method append() give list in the list. ['Bartin', 'Natasha'] consider as a list in the existing list. But the method extend() add the given list in the existing list.

count()

Syntax
list.count(item)

The method count() used to find the occurrence of item in the list. Let's check the example below
Find occurrence of item from python list
list count
Example shows character 'c' occurs 3 times.

index()

Syntax
list.index(item)

Consider a big list and you want to know the index of particular item then use method index(). Let's check the example below
Finding index of python list
list Index
If an item occurs 2 or more than 2 times it'll give you index of first occurrence of item.

insert()

Syntax
list.insert(index, item)

index This is the Index where the item need to be inserted. item This is the item to be inserted into the given list. Example
Inserting item at particular index in python list
list Insert
So in above example, I forget to put Iron-man and I want to keep Iron-man after Captain , in this situation I used insert() and pass 1 index and Iron-man as item.

pop()

Syntax
list.pop()

The method pop() removes and returns last item from the list. Example 1
pop() in python list
list pop
In above example pop() function remove last item.
Example 2
pop() in python list
list pop
In above example we passed number to pop() method, so it remove the item with index 2 from the list.

remove()

Syntax
list.remove(item)

The method remove() remove the item from the list.
Example 1
Removing item from python list
list remove
In above example Loki is removed by remove() method
Example 2
Removing first occurrence of item from python list
Remove first occurrence from list
In above example method remove only first occurrence of 4 from the list.


In programming world many time, we want to reverse the list, for this you use for loop and make some complex code but in python one method can do this stuff.

Sort()

Syntax
list.sort(cmp=None, key=None, reverse=False)

The method sort() sort the items of the list in place, Python’s sort is stable; the order of items that compare equal will be preserved.
sort the python list
list sort
First it sort the numeric values than it goes to strings value. So this method sort() changes the original list.

reverse()

Syntax
list.reverse()

The method reverse() reverse the item of list,in place.
Reverse the python list
list reverse
So this program change the original list.



Python List Comprehensions

Python List comprehensions is a concise way to create lists.
Consider a simple example, list contain square of numbers.
squ = []
for x in xrange(1,6):
   squ.append(x**2)
print squ
Out-put
Python List comprehensions
Python list by for loop
You can do the same thing by Python list comprehensions.
Python List comprehensions
Python list Comprehensions
for more details you can refer the link https://docs.python.org/2/tutorial/datastructures.html

Lists are mutable. They can change. This makes lists the most powerful and flexible sequence type at your disposal.





admin