Lists in Python
Making a List, Concatenation of Lists, Length of List ,Sum,Max,Min
List are array in python. It stores data in it and data can be changed in it . In python lists are very important . List save data on run time . It means that if you have pass list in a loop than it will store all data with in it.OR
You were thinking that why use list,what it will make difference in programming.Show think for a sec that you have to store 100 values in program. What you will you do , will you make 100 of variable for storing the data.
Will you ? No in programming it is not recommended . So here is list you can save your desired data and can access any one in that . Python have many function which directly programming in dealing with list. Look at programm now because “Talk is cheap. Show me the code.” – Linus Torvalds.
Declaration of list in Python
l1= print("This is how it look like",l1)
For creating list just put a variable name and assign with square bracket.This simple step only create list in python.Here is output of above program.
This is how it look like= 
Now list with some numeric value and concatenation of lists.
Concatenation is adding two list in one but this does not mean that it will add like 2+3=5
Let’s see how to put value in list.Values in list is given in square bracket and are separated by comma
like this l=[1,2,3,4]. Now you have created list let’s concatenation it in below program.
''' concatenation in Python list on List ''' l0= # Give some values in it by separating comma l0=[1,2,3,4] l1=[4,6,8,9] l2=l0+l1 print("This is contacinate of two list",l2)
Output of the above program
This is contacinate of two list [1, 2, 3, 4, 4, 6, 8, 9]
More program to understand the contacinate of list
''' Giving values in list in left side and right side of list ''' l=[0,5,4,2] l=l+ print("List after adding value in right",l) l=+l print("List after adding values in left",l) l=[1,2]+l+[1,2] print("List after adding values in both side",l)
Above program is showing that if you concatenate values in left it will go in left if you concatenate values in right if will be in right.Now see the output of the above program
List after adding value in right [0, 5, 4, 2, 5] List after adding values in left [5, 0, 5, 4, 2, 5] List after adding values in both side [1, 2, 5, 0, 5, 4, 2, 5, 1, 2]
Now Sorting in list
Now you are familiar with how to declare list in python . Let’s do some shorting in it .If you don’t know what is shorting , here is it’s simple definition .Shorting means arranging values in ascending or in descending order.
l1=[1,4,2,3,0] print("Unsorted list ",l1) l1.sort() print("After sorting list order is ",l1)
In above program we have done sorting .Let’s elaborate the program. First we declare the list by putting unordered values in it .Then we print list, which is list l1. Now add a function sort() to that list which you want to sort
then in last step print that list which is attached with sort function .See below
Output of program
Unsorted list [1, 4, 2, 3, 0] After sorting list order is [0, 1, 2, 3, 4]
Now we will do Sorting of strings . Remember sorting of string in list is done on the basis of number of alphabet
A is first and Z is last .Look at program now
l3=["Saubhagya","Abhay","Umang","Champak"] print("Unsort String",l3) l3.sort() print("Sorted String",l3)
Output of program is
Unsort String ['Saubhagya', 'Abhay', 'Umang', 'Champak'] Sorted String ['Abhay', 'Champak', 'Saubhagya', 'Umang']t
Finding the length of the list .Code is below
''' Lenght of list ''' l=[0,5,4,2] l=len(l) print("Length of string is =",l)
Look the whole code is known except that function len().You only have to pass the list in that function and you will get the length of the list.Yes in python you will get functions like this which make programming easier.Look
at output of program
Length of string is = 4
How can len() function help in your programming . Suppose you have make a program which stores record of student on daily attendance basis. Now you are asked for total number of student present in school or in specific class.
So you have to just pass the list through len() function and you will get the length which will always equal to number of student.
These are some basic uses of list in python or you can say that a basic guide.