List Data Structure
If we want to represent a group of individual objects
as a single entity where insertion order preserved and duplicates are allowed,
then we should go for List.
Properties of List:
· insertion order preserved.
· duplicate objects are allowed
· heterogeneous objects are allowed.
· List is dynamic because based on our requirement we
can increase the size and decrease the size.
· List objects are mutable i.e. we can change the
content.
Syntax:
List1
= [element1, element2, element3, ……….]
In List, the elements will be placed within square
brackets and separated with a comma separator.
By using an index:
We can differentiate duplicate elements
and we can preserve insertion order
Hence the index will play a very important role.
Python supports both positive and negative indexes.
· positive index means from left to right
· negative index means right to left
Example: l = [10,20,30,40,50]
Creation of List Objects:
1.
Creation of an
empty List:
list=[]
print(list) #----> []
print(type(list))#---> <class
'list'>
2. If we know elements
already then we can Hard Code the values
list=[10,20,30,40]
3. With dynamic input:
We can take the input at run time by the user, such input is called Dynamic Input
Dynamic input by default is in the string format, so we can convert it into our required format using eval() function
eval() evaluates the type of input by the user and returns the value in the evaluated type. Therefore eval() can help us convert the user input into a list.
list = eval(input("Enter
a List of Numbers:"))
Enter a List of Numbers: [111,222,333,444,555]
print(list) #---------> [111,222,333,444,555]
print(type(list)) #---------->
<class 'list'>
4. With list() function: list() is a type casting function which can convert data of other types into a list.
r=range(0,11,2)
print(type(r)) #---------------->
<class ‘range’>
l=list(r)
print(l) #--------> [0,2,4,6,8,10]
print(type(l)) #-----------> <class
'list'>
Eg with string:
s="Python"
l=list(s)
print(l) #------->['P', 'y', 't', 'h', 'o',’n’]
5. with split() function:
s="Learning Python is very easy "
l=s.split()
print(l) #----------> ['Learning',
'Python', 'is' , 'very', 'easy']
print(type(l)) #--------> <class
'list'>
Accessing elements of List:
We can access elements of the
list
· by using index
· by using slice operator(:)
1. By using an index:
List follows zero-based
index.
ie index of the first element is
zero.
The list supports both positive
and negative indexes.
positive index meant for Left
to Right
Negative index meant for
Right to Left
2. By using the slice operator:
Syntax: list2= list1[start:stop:step]
start ------>
indicates the index where slice has to start, the default value is 0
stop ------>
indicates the index where slice has to end, the default value is the maximum allowed
index of list
i.e. length of the list
step -------->
increment value, the default value is 1
Remember the chart for slice operator we discussed in string datatype..?
it's applicable for lists as well.
Traversing the elements of
List:
The sequential access of each
element in the list is called traversal.
1. By using while loop:
n=[0,1,2,3,4,5,6,7,8,9,10]
i=0
while i<len(n):
print(n[i])
i=i+1
Output:
0
1
2
3
4
5
6
7
8
9
2. By using for loop:
n=[0,1,2,3,4,5,6,7,8,9,10]
for n1 in n:
print(n1)
Output:
0
1
2
3
4
5
6
7
8
9
Nested List:
We
can also store a list inside the list , this is called as Nested List
List1=[10,20]
List2=[30,40]
The
above two are individual lists, we can store these two lists in another list
List3
= [List1,List2]
No comments:
Post a Comment