Python list are containers that are used to store a list of values of any type. Unlike other variable Python lists are mutable you can change the the elements of a list in place: Python will not create a fresh list when you make changes to an element of a list . List is a type of sequences like strings and tuples but it differs from them in the way that list are mutable but strings and tuples are immutable.
Creating and accessing Lists
# creating and accessing list:
list1=[] #empty list
list2=['a','s','d','f','g','h'] # list of characters
list3=['1',2,3,4,5,6] # list of integers
list4=["Star","Tick","musicaly","Videos","Audios"]
list5=['a',1,'e','r','t','y',3.0] # mixed type list
print("Empty list",list1)
print("list of characters",list2)
print("list of integers",list3)
print("mixed type list",list5)
print("String",list4)
Output of this code:
Empty list []
list of characters ['a', 's', 'd', 'f', 'g', 'h']
list of integers ['1', 2, 3, 4, 5, 6]
mixed type list ['a', 1, 'e', 'r', 't', 'y', 3.0]
String ['Star', 'Tick', 'musicaly', 'Videos', 'Audios']
Second method to define a list
L= list()
print("list()=",L)
L= list("hello python")
print("list(hello python)=",L)
#L1= list("hello python","hello 2") #this will generate an error list expected at most 1 argument.
#print("list(hello python)=",L1)
# we can assign multiple value to list
list1=("1","2","3","4",'a','d','1') # you can write
list2 =list(list1)
print(list2)
OutPut of the above Program:
list()= []
list(hello python)= ['h', 'e', 'l', 'l', 'o', ' ', 'p', 'y', 't', 'h', 'o', 'n']
['1', '2', '3', '4', 'a', 'd', '1']
List value entered by the user:
list1=list(input ('Enter list elements='))
print(list1)
output of the above program:
Enter list elements=123kjh
['1', '2', '3', 'k', 'j', 'h']
Using above way the data type of all characters entered is string even though we entered
digits. To enter a list of integers through keyboard, you can use the methods given below.
#eval
list1= eval(input("Enter list to be added: "))
print(list1)
print("eval", eval("12+15"))
print("eval", eval("12+1+5"))
print("eval", eval("12*15"))
print("eval", eval("12/15"))
Out Put of the above code:
Enter list to be added: 123
123
eval 27
eval 18
eval 180
eval 0.8
Accessing lists: Lists are mutable (editable) sequences having a progression of elements. There must be a way to access its
individual elements and certainly there is. similarly with strings list are sequences just like strings as they also index their individual elements, just like strings do. as shown in figure
We can access the list elements just like you access a strings elements list[i] will give you the elements at ith index of the list; List[a:b] will gives you elements between indexes a to b-1 and so on.
Membership operator: in and not in
Concatenation(+) replication(*) operation
Traversing a list : traversal of a sequence means accessing and processing each element of it.
Thus traversing a list also means the same and same is the tool for it.Thus is why sometimes
we call a traversal as looping over a sequences
list1=['a','s','d','f','g','h']
print("list1[1]",list1[1])
print("list1[2]",list1[2])
print("list1[4]",list1[4])
print("list1[-1]",list1[-1])
print("list1[-5]",list1[-5])
print("list1[5]",list1[5])
OUTPUT:
list1[1] s
list1[2] d
list1[4] g
list1[-1] h
list1[-5] s
list1[5] h
How we use membership operator: in
l=['g','3','2','1','s','a']
# defination of the for loop
for a in l :
print(a)
output of this program:
g
3
2
1
s
a
========================================================
l=['g','3','2','1','s','a']
# defination of the for loop
length=len(l)
for a in range(length) :
print(a,"-->",l[a])
Output of this program
0 --> g
1 --> 3
2 --> 2
3 --> 1
4 --> s
5 --> a
Comparing Lists
We can compare two lists using standard comparison operators of python, Python internally compares individual elements of list.
l2,l1=[2,3,2,4,7],[2,3,2,5,7]
#l1==l2
l3=[1,[2,3]]
l1<l2
Out Put
False
============================================================
a=[2,3]
b=[2,3]
c=['2','3']
d=[2.0,3.0]
e=[2,3,4]
#a==c
print("a==c",a==c)
print("a==b",a==b)
print("a>b",a>b)
print("d>a",d>a)
print("d==a",d==a)
print("a<e",a<e)
output of this code
a==c False
a==b True
a>b False
d>a False
d==a True
a<e True
=================================================================================
list1= [1,2,3]
list2=[6,7,8]
print("list1+list2",list1+list2)
print("list1*3",list1*3)# replicating lists
print("list1*3",list1*5)# replicating lists
print("list1+2",list1+2) # error
print("list1+abc",list1+"abc")# error
output of this code
list1+list2 [1, 2, 3, 6, 7, 8]
list1*3 [1, 2, 3, 1, 2, 3, 1, 2, 3]
---------------------------------------------------------------------------
=============================================================================================
lists also support slice steps.That is if you want to extract, not consecutive but every other elements of the list,
Slice steps.
list[start:stop:step]
list1= [1,2,3,3,4,5,6,7,8,9]
list2=[6,7,8]
print("list1+list2",list1+list2)
#print("list1*3",list1*3)# replicating lists
#print("list1*3",list1*5)# replicating lists
print(list1[0:7:2]) #list[start:stop:step]
output of this code
list1+list2 [1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 6, 7, 8]
[1, 3, 4, 6]
===============================================================================
list1= [1,2,3,3,4,5,6,7,8,9]
list2=[6,7,8]
print("list1+list2",list1+list2)
#print("list1*3",list1*3)# replicating lists
#print("list1*3",list1*5)# replicating lists
print("list1[0:7:2]",list1[0:7:2])
print("list1[:7:2]",list1[:7:2])
print("list1[0::2]",list1[0::2])
Output of this code
list1+list2 [1, 2, 3, 3, 4, 5, 6, 7, 8, 9, 6, 7, 8]
list1[0:7:2] [1, 3, 4, 6]
list1[:7:2] [1, 3, 4, 6]
list1[0::2] [1, 3, 4, 6, 8]
======================================================================================================================
list2=[2,3,4,5,6,7,8,9]
list1=list2
sum=0
for a in list1:
sum+=a
print("Sum=",sum)
output of this code
Sum= 44
=============================================================
List modification
l=["one","two","three"]
l[0:2]=[2,3]
print(l)
l[2:]="25"
print(l)
l[2:]="2585"
print(l)
[2, 3, 'three']
[2, 3, '2', '5']
[2, 3, '2', '5', '8', '5']
====================================================================================
There is another way of doing it, creating the true copy of a list, by using the
copy() method. You can use the copy() method as per this syntax:
list1.copy()
la=[11,12,12,13]
lb=la.copy()
print(lb)
lb[0]=lb[0]+10
print(lb)
output of the code
[11, 12, 12, 13]
[21, 12, 12, 13]
=================================================================
In this section we learn about len() function which returen lenght the list
index of the element, append() and extend()
append function adds one element to a list, extend() can add multiple elements from a list supplied to it as argument.
as shown in this porgram code
list1=[1,2,3,4,5,6,7,8,9,0]
list2=[2,3,4,5,6,7,8,9,0,1]
print("len()=", len(list1))
print("list1.index(6)",list1.index(6))
list1.append('blue')
print("list1.append('blue')=",list1)
list1.extend(list2)
print("list1.extend(list2)",list1)
list1.append([2,3,4,5])
print(list1)
output of the above code
len()= 10
list1.index(6) 5
list1.append('blue')= [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 'blue']
list1.extend(list2) [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 'blue', 2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 'blue', 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, [2, 3, 4, 5]]
#insert
list1=['a','s','d','f']
list1.insert(2,'r')
print("list1.insert(2,'r')",list1)
list1.insert(len(list1),"last element")
print("list1.insert(len(list1),last element)",list1) # same as append function
list1.pop(len(list1)-1) #need index as argument
print("after pop function",list1)
list1.remove('f')# need value as argument
print("after remove function",list1)
list1.clear()
print("list1.clear()",list1)
del list1
print("list1 del",list1) # del remove the list1 that why error occurs
list1.insert(2,'r') ['a', 's', 'r', 'd', 'f']
list1.insert(len(list1),last element) ['a', 's', 'r', 'd', 'f', 'last element']
---------------------------------------------------------------------------
=========================================================================================================================
list1=['a','s',2,2]
list1.insert(2,'r')
print("list1.insert(2,'r')",list1)
list1.insert(len(list1),"last element")
print("list1.insert(len(list1),last element)",list1) # same as append function
list1.pop(len(list1)-1) #need index as argument
print("after pop function",list1)
'''
list1.remove('s')# need value as argument
print("after remove function",list1)'''
#list1.clear()
#print("list1.clear()",list1)
#del list1
#print("list1 del",list1) # del remove the list1 that why error occurs
print("list1.count(s)",list1.count('s'))
print("min(list1)",min(list1))
print("max(list1)",max(list1))
list1.insert(2,'r') ['a', 's', 'r', 2, 2]
list1.insert(len(list1),last element) ['a', 's', 'r', 2, 2, 'last element']
after pop function ['a', 's', 'r', 2, 2]
list1.count(s) 1
min(list1) 2
max(list1) 7
python list and its functions python modules list and their functions python list the functions in a module python list and function python list function arguments python list function attributes python list function args python list function append python list function average python list function add python list function count python list function in module python list function index python list function in class python list function input python list function iterate list and its default function in python python list function module python list function return python list function remove python list function replace python list function sort
0 Comments