THe List In a python it store a collection of value of multiple type. and the list contains the collection of value stored in the single variable. then we access that variable in the python code . The list contains the heterogenous typed values. The values is called list elements and items in the python. list is allows a you to store the collection of value of any type it make python pawer full tool. some list Importance in the below
As we saw in string the list is similar to string but some rules and python list syntax is different
The List Is a ordered and mutable collection of data .
The list Contain the collection of elements in different types.
We accessing that list value by using one technique is called Indexing
The Python list is allows a you to access the individual elements by passing the index value
The list contain the collection of data items that is enclose within the square brackets[] and each element is separated with commas
We want to create a python list . by placing the collection elements in the square brackets and separated with comma , The Python provide the list to store the multiple typed data in the single variable. the list is a ordered and mutable which means we can change the list elements after that creating by using index value
Variable_name=[ value1,value2,... value-n]
# some print code here
my_List=[10,10.2,"hello"]
print(my_List)
10
10.2
hello
After creating the list we must be access that list in the python code. when we want to access the items in the list we must be know the indexing . using indexing to access the elements from the list . the list first element index is 0 and the list last element index is n-1 . n is number of elements in the list. passing the index value in this way . variable_name that is list name and slice operator that is [:] . inside the slice operator place index. Example in the below
List=["hello", 10, 20,30,40.2]
print(List[0:2])
[hello ,10 ,20 ]
The is Another way to access the elements from the list . negative indexing means it start from last element of list is represented by -1 and the the first element is represented by -n means the number of elements in the list . when we are using this negative indexing in the since we want to access the list elements from right to left. the Example in the below
list=[10,20,30,"hello","Darshan"]
print(list[-3:-1])
[30, 'hello']
String | List |
---|---|
The String is a sequence of character | The List is a Sequence of different data items or Elements |
The String is a Immutable which means once the string is created we cannot bew change | The List is a mutable which means we can change the list elements after creating a list |
The String is accessing using index value or Indexing | The List elements also accessing using the indexing |
The python list is a mutable which means we can change that list element after creating a list. when we want to change the list element we must be used the index. as below example
list=[10,20,30,"hello","Darshan"]
list[4]="Nandini"
print(list)
[10, 20, 30, 'hello', 'Nandini']
The List is a iterable which means we using the for loop to print the each element in the list as sequentially . traversing means we print the list elements in one by one . Example in the below
list=[10,20,30,"hello","Darshan"]
for i in list:
print(i)
10
20
30
hello
Darshan
Concatenating is a process of of adding the two list . and adding the new elements to the list . by using + operator to adding the two list to gather. syntax in the below
list=list1+list2+........+list n
list1=[10,20,30,"hello"]
list2=[40,50,60,"darshan"]
list=list1+list2
print(list)
[10, 20, 30, 'hello', 40, 50, 60, 'darshan']
The adding the elements by using append() or insert(index) function
The adding the new element to list by using append() it does not assigning index
list1=[10,20,30,"hello"]
list1.append("Darshan")
print(list1)
[10, 20, 30, 'hello', 'Darshan']
Adding the new list element by specifying the index to store the new list element as show in the below example
list1=[10,20,30,"hello"]
list1.insert(3,"darshan")
print(list1)
[10, 20, 30, 'darshan', 'hello']
The removing the existing list or list element by using remove(value) function. as shown in the below example
list1=[10,20,30,"hello"]
list1.remove(20)
print(list1)
[10, 30, 'hello']
orlist1=[10,20,30,"hello"]
del list1
print(list1)
repeating the list by using the * (asterisk) operator . we can also assign the repetition value . as shown in the below example
list1=[10,20,30,"hello"]
print(list1*3)
[10, 20, 30, 'hello', 10, 20, 30, 'hello', 10, 20, 30, 'hello']
The membership is used to we check the membership of an element from a list using membership operator That is membership in and membership not in . the membership operator checks the particular list element is belongs to the list or not
It Checks the particular element is belongs to list or not . if that particular element is belongs to list then the output is true other wise false
list1=[10,20,30,"hello"]
result=10 in list1
print(result)
True
It is offsite to membership in
list1=[10,20,30,"hello"]
result=10 not in list1
print(result)
False
The comparison is a process of compare the two lists and return the value . you want to learn the comparison operators please go through the operator lesson . the comparison value should be boolean value either True OR False
Example In the Below
list1=[10,20,30]
list2=[20,40,30]
print(list1<
print(list1>list2)
print(list1==list2)
print(list1<=list2)
print(list1>=list2)
print(list1!=list2)
True
False
False
True
False
True
THe Coping the list is a process of assigning the list variable element to another variable as shown in the below example
list1=[10,20,30]
list2=list1
print(list2)
[10, 20, 30]