List
A List is a way to store multiple types of values.
The value given inside the List is called List item.
A comma(,) should be used to separate each List item.
List is a mutable data type. The reason is that we can change the value given in List, so it is called mutable data type.
List is enclosed in square brackets.
Ex:
details=[“good”,5.5,20]
List Index
We have already seen the concept of index in string. We can get the value in the list only by using index.
details=["good",3.3,50]
print(details[0])
print(details[1])
print(details[2])
''''
o/p:
good
3.3
50
''''
Explanation:–
details[0] prints the string good as the first value in the list. details[1] refers to the second floating value of 3.3. details[2] prints the third value 50.
num=[10,20,30,[40,50],60]
print(num[0])
print(num[3][0])
''''
o/p:
10
40
''''
Description:-
[40,50] This is a list.
If num[0] is given then the first value 10 will be taken.
num[3,0] -> In this there is a list at index position num[3]. Index number [0] is given to pick the first value “40” in that list.
details=["html","css","java"]
details[1]="reactjs"
print(details)
''''
o/p:
['html', 'reactjs', 'java']
''''
Description:-
A list is stored in the variable called details. There are 3 items in that list.
details[1] = “reactjs” -> This statement removes the CSS that is the second item in the list and replaces it with “react js”.
names=["c","c++","angular"]
del names[2]
print(names)
''''
o/p:
['c', 'c++']
''''
Description:-
del name[2]-> In this statement, there is value angular at name[2]. After removing that value and then printing the name variable [“C”,”C++”] these two items are displayed.
Only one value in the list can be deleted and modified using Index.
Slicing concept is used to delete and modify multiple values.
List slicing
Using the concept of list slicing, you can select only a specific part of the list.
number=[10,20,30,40,50]
print(number[0:3])
''''
o/p:
[10, 20, 30]
''''
Slice Method
frontend=["html","css","js","reactjs"]
backend=["jsp","php","nodejs","c#"]
result=slice(0,2)
print(frontend[result])
print(backend[result])
''''
o/p:
['html', 'css']
['jsp', 'php']
''''
Description:-
slice is a build in method.
slice(0,2) tells us to take values from 0 to the index before 2.
The value taken in this way should be stored in the variable called result.
If we use the result variable in any list, it separates the value from 0 to 2 in that list.
no=[10,20,30,40,50,60]
print(no[::-1])
''''
o/p:
[60, 50, 40, 30, 20, 10]
''''
Explain:-
no[::-1] -> This will print the full list as start and end positions are not given, but we have given -1 at the end. So list is printed from reverse.
num=[10,20,30,40,50,60,70,80]
num[0:4]=[1,2,3,4]
print(num)
''''
o/p:
[1, 2, 3, 4, 50, 60, 70, 80]
''''
Description:-
num[0:4] = [1,2,3,4] -> In this statement remove the value starting from index 0 to index 4 and replace it with [1,2 ,3,4] will append the value to the list.
num=[10,20,30,40,50,60,70,80,90]
num[::2]=[1,1,1,1,1]
print(num)
''''
o/p:
[1, 20, 1, 40, 1, 60, 1, 80, 1]
''''
Description:-
num[::2] takes all values as start and end positions are not given here. 2 of which removes the value for every 2 steps and replaces it with a value of one.
num=[10,20,30,40,50,60,70]
del num[0:4]
print(num)
''''
o/p:
[50, 60, 70]
''''
Description:-
del num[0:4] -> Here it removes the value from 0 to before 4th position and prints the remaining value.