List
List : It is a standard data
type of python which can
store multiple values in one
variable it is just like Array in
OPPs languages.
Creating List in Python
To create list in python put number
of expressions, separated by
commas in square bracket e.g.
X = [22,24,29,27]
X has stored four values
We can also create empty list.
Creating List From keyboard
Python
To create list in python from input
method user can insert value on its own.
As shown in e.g. below.
>>>li=list(input(“Enter list Values”))
Enter list values 134567 #values Entered.
>>>li
[‘1’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’] #values Printed.
List Operations.
Traversing a list : It means accessing and
processing each element of list . We can use for
loop for this as shown in e.g.
Li = [ ‘E’, ‘X’, ‘A’, ‘P’, ‘L’, ‘E’]
for x in Li: # a is looping variable & Li contain list.
print(a) # this will print result.
The result of this program will be:
E
X
A
M
P
L
E
Joining List : This is just like +
operator.
It is used to add two list.
e.g. of joining list in python.
>>> v1 = [1,3,5]
>>> v2 = [7,9,11]
>>> v1+ v2
[1,3,5,7,9,11]
(These commands can be performed in shell &
interactive both modes.)
Repeating or Replicating: This is just
like * operator which can replicate a
list desired no. of times.
e.g. of joining list in python.
>>> v1 = [1,3,5]*3
[1,3,5, 1,3,5, 1,3,5]
(These commands can be performed in shell &
interactive both modes.)
Slicing list: Extracting a part of list from
complete list. It is done through indexing.
e.g. of Slicing list
>>> L1= [10,12,14,20,22,24,30,32,34]
Seq: This command we will use to extract items from
list, from certain position, but it counts from 0 (zero) as
indexing is done from 0 (zero). e.g.
Seq=L1[3: -3] # 3 is the position from where to print
and -3 to stop. Here - sign counts from end of list. So
result will be as follow.
>>>L1
[20,22,24]
Contined.
Slicing list: Extracting a part of list from
complete list. It is done through indexing.
e.g. of Slicing list
seq=<List variable>[start: stop: step]
>>> L2=[10,12,14,16,18,20,22] #L2 is new list
>>> L2[0:12:2] # start from 0 and take jump of 2
[10, 14, 18, 22]
>>> L2[2:10:3]
[14, 20]
(You can change position and check more
examples)