Python List | List in Python
Python Lists are containers that are used to store a list of values of any type.
Lists are mutable.It means you can change the elements of a list.
Python will not create a separate list when you make changes to an element of a list.
A list is a datatype of Python that can store a sequence of values of any datatype.
Note Lists are similar to strings in many ways like indexing,slicing and accessing individual elements but they are different in the sense that Lists are mutable and Strings are immutable.
Python Lists are containers that are used to store a list of values of any type.
Lists are mutable.It means you can change the elements of a list.
Python will not create a separate list when you make changes to an element of a list.
A list is a datatype of Python that can store a sequence of values of any datatype.
How to create list in python ?
Empty List
The empty list is []. It is the list equivalent of 0 or ' ' and like them it also has truth value as false. You can also create an empty list as :
L=list()
or
L=[]
It will generate an empty list and name that list as L.
The empty list is []. It is the list equivalent of 0 or ' ' and like them it also has truth value as false. You can also create an empty list as :
L=list()
or
L=[]
It will generate an empty list and name that list as L.
Mixed List
The mixed list contains mixed type of elements.
You can also create mixed list as :
L=[2,3,4,'P',3,4.5,6.7,'Python',3,2,9,2]
A Mixed list can have integer,float and string datatypes elements.
You can also create mixed list as :
L=[2,3,4,'P',3,4.5,6.7,'Python',3,2,9,2]
A Mixed list can have integer,float and string datatypes elements.
Nested List
Creating Lists from Existing Sequences
You can also create list from existing sequences like string, tuple and lists.
L=list(<string|tuple|list>)
for example:
L=list("Hello")
print(L)
tup=(2,3,4,5)
L=list(tup)
print(L)
We can also create list using input method by user.
L=list(input("Enter List"))
Note if you create a list entered by a user using keyboard. The datatype of all the entered elements is String even though user entered digits.
If you want to enter a list of integers through keyboard, you can use the method given below:
L=eval(input("Enter list"))
print(L)
Please note sometimes eval() does not work in python shell. At that time you can run it on a script.
How to access list | Indexing Lists
Lists are sequences just like strings.They also index their individual elements just like strings do i.e., forward indexing as 0,1,2,3,..... and backward indexing as -1,-2,-3,...
Thus, you can access the list elements just like you access a python strings's elements.
for example:
L=[2,3,4,5,5]
print(L[])
print(L[:])
L[i] will give you the element at ith index of the list .
L[a:b] will give you elements between indexes a to b-1 and so on...
As mentioned above image , the individual elements of a list are accessed through their indexes.
Note if you give index outside its range while accessing individual elements. Python will raise Index Error like strings.
Lets take nested list example how to access elements:
L=[2,3,4,[3,5],'hello',6.7,8.9,'p',['python']]
print(L[3])
print(L[4])
print(L[6])
print(L[8])
You can also create list from existing sequences like string, tuple and lists.
L=list(<string|tuple|list>)
for example:
L=list("Hello")
print(L)
tup=(2,3,4,5)
L=list(tup)
print(L)
We can also create list using input method by user.
L=list(input("Enter List"))
Note if you create a list entered by a user using keyboard. The datatype of all the entered elements is String even though user entered digits.
If you want to enter a list of integers through keyboard, you can use the method given below:
L=eval(input("Enter list"))
print(L)
Please note sometimes eval() does not work in python shell. At that time you can run it on a script.
How to access list | Indexing Lists
Lists are sequences just like strings.They also index their individual elements just like strings do i.e., forward indexing as 0,1,2,3,..... and backward indexing as -1,-2,-3,...
Thus, you can access the list elements just like you access a python strings's elements.
for example:
L=[2,3,4,5,5]
print(L[])
print(L[:])
L[i] will give you the element at ith index of the list .
L[a:b] will give you elements between indexes a to b-1 and so on...
Note if you give index outside its range while accessing individual elements. Python will raise Index Error like strings.
Lets take nested list example how to access elements:
L=[2,3,4,[3,5],'hello',6.7,8.9,'p',['python']]
print(L[3])
print(L[4])
print(L[6])
print(L[8])
How to modify list ?
Slicing Lists
List slices, like string slices are the sub part of a list extracted out. You can use indexes of list elements to create list slices.
for example :
L=[1,2,3,4,5]
print(L[3:5])
Slicing Lists
List slices, like string slices are the sub part of a list extracted out. You can use indexes of list elements to create list slices.for example :
L=[1,2,3,4,5]
print(L[3:5])
No comments:
Post a Comment