Let's clearly see the list's built-in methods.
len()
The length of the list can be calculated using the len() method.
a=[1,2,3,"good"]
print(len(a))
''''
output:
4
''''
Since there are 4 elements inside the list called len(a), we see 4 on the output screen.
Concatenation
List Concatenation is the joining of two or more Lists together into a single List.
The (+) operator is concatenation.
x=[1,2,3,4]
y=[5,6,7]
print(x+y)
''''
output:
[1, 2, 3, 4, 5, 6, 7]
''''
Description:-
A list of four items is stored in the variable x.
A list of three items is stored in the variable y.
With the help of concatenation operator + in the statement x + y, the 2 lists x and y are joined together and converted into a single list.
List repetition
List repetition means that the items in the list should be repeated as many times as the given value.
x=[1,2,3]
print(x*2)
''''
output:
[1,2,3,1,2,3]
''''
Description:-
Repeats the list items at x twice.
append()
The append method adds an element to the end of the list.
x=["html","css","js"]
x.append("react")
print(x)
output:
["html","css","js","react"]
Explain:-
x.append(“react”) -> Adds react to the last value in the list x.
extend()
The extend() method appends the given element to the end of the list.
x=["c","c++","java"]
x.extend("php")
print(x)
''''
output:
['c', 'c++', 'java', 'p', 'h', 'p']
''''
Description:-
x.extend(“php”) -> In this statement appends the last value “php” in the list x. Let's call iterate each character.
x=["c","c++","java"]
x.extend(20)
print(x)
''''
output:
Traceback (most recent call last):
File "main.py", line 11, in <module>
x.extend(20)
TypeError: 'int' object is not iterable
''''
Description:-
extend(20) -> where 20 is a numerical value. An error occurs because the numerical value cannot be iterated.
x=["c","c++","java"]
x.extend("20")
print(x)
''''
output:
['c', 'c++', 'java', '2', '0']
''''
Description:-
extend(“20”) -> Here 20 is a string value so it can be iterated.
a=["c","c++"]
b=["js","php"]
a.append(b)
print(a)
''''
output:
['c', 'c++', ['js', 'php']]
''''
Explain:-
a.append(b) -> refers to list b inside list a. It looks like a nested list.
x=["c","c++"]
y=["js","php"]
x.extend(y)
print(x)
''''
output:
['c', 'c++', 'js', 'php']
''''
Explain:-
x.extend(y) -> concatenates list y inside list x. But here it returns as a single List.
Insert()
A value can be inserted anywhere inside the list using the Insert() method.
Syntax:-
list.syntax(position, element)
Position -> position indicates at which index position we are going to give.
Element-> Indicates the type of Element we are going to render.
x=["c","c++","java","python"]
x.insert(1,"php")
print(x)
''''
output:
['c', 'php', 'c++', 'java', 'python']
''''
insert(l,”php”) -> where l is index position. "php" is a string value. That means we insert the value "php" in index position 1st.
pop()
Pop() method is used to remove a specific element in the list using its position.
Syntax:-
list.pop(position);
x=["c","c++","java","php"]
x.pop()
print(x)
''''
output:
['c', 'c++', 'java']
''''
Description:-
x.pop() -> This statement removes the last value in the list x using the pop method.
x=["c","c++","java","php"]
x.pop(1)
print(x)
''''
output:
['c', 'java', 'php']
''''
Description:-
x pop(1) -> This statement removes the value “C++” from index position x in the list x.
remove()
A particular value can be removed from the list using the remove() method.
x=["c","java","js"]
x.remove("js")
print(x)
''''
output:
['c', 'java']
''''
Description:-
x.remove(“Js”) -> Removes the Js element in the x list.
x=["c","js","php","js"]
x.remove("js")
print(x)
''''
output:
['c', 'php', 'js']
''''
Description:-
x.remove(“Js”) -> There are two “Js” elements in list x. This remove method only removes the first “Js”.
clear()
The clear() method removes all elements from the list.
x=["c","js","php","js"]
x.clear()
print(x)
''''
output:
[]
''''
index()
Returns the Index value of a particular element in the List.
x=["c","js","php","js"]
print(x.index("php"))
''''
output:
2
''''