python tutorial - Python List | List methods in Python - learn python - python programming




Adding and Appending

  • append(): Used for appending and adding elements to List.It is used to add elements to the last position of List.

Syntax:

python - Sample - python code :

list.append (element)
click below button to copy the code. By Python tutorial team

python - Sample - python code :

# Adds List Element as value of List.
List = ['Mathematics', 'chemistry', 1997, 2000]
List.append(20544)
print(List)
click below button to copy the code. By Python tutorial team

python programming - Output :

['Mathematics', 'chemistry', 1997, 2000, 20544]

 

  • insert(): Inserts an elements at specified position.

Syntax:

python - Sample - python code :

list.insert(<position, element)
click below button to copy the code. By Python tutorial team

Note: Position mentioned should be within the range of List, as in this case between 0 and 4, elsewise would throw IndexError.

python - Sample - python code :

List = ['Mathematics', 'chemistry', 1997, 2000]
# Insert at index 2 value 10087
List.insert(2,10087)    
print(List)
click below button to copy the code. By Python tutorial team

python programming - Output :

['Mathematics', 'chemistry', 10087, 1997, 2000, 20544]
  • extend(): Adds contents to List2 to the end of List1.

Syntax:

python - Sample - python code :

List1.extend(List2)
click below button to copy the code. By Python tutorial team

python - Sample - python code :

List1 = [1, 2, 3]
List2 = [2, 3, 4, 5]
 
# Add List2 to List1
List1.extend(List2)     
print(List1)
 
#Add List1 to List2 now
List2.extend(List1) 
print(List2)
click below button to copy the code. By Python tutorial team

python programming - Output :

[1, 2, 3, 2, 3, 4, 5]
[2, 3, 4, 5, 1, 2, 3, 2, 3, 4, 5]

sum(), count(), index(), min() and max() functions of List

  • sum() : Calculates sum of all the elements of List.

Syntax:

python - Sample - python code :

sum(List)
click below button to copy the code. By Python tutorial team

python - Sample - python code :

List = [1, 2, 3, 4, 5]
print(sum(List))
click below button to copy the code. By Python tutorial team

python programming - Output :

15

What happens if numeric value is not used a parameter?
Sum is calculated only for Numeric values, elsewise throws TypeError.
See example:

python - Sample - python code :

List = ['gfg', 'abc', 3]
print(sum(List))
click below button to copy the code. By Python tutorial team

python programming - Output :

Traceback (most recent call last):
  File "", line 1, in 
    sum(List)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
  • count():Calculates total occurrence of given element of List.

Syntax:

python - Sample - python code :

List.count(element)
click below button to copy the code. By Python tutorial team

python - Sample - python code :

List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(List.count(1))
click below button to copy the code. By Python tutorial team

python programming - Output :

4
  • index(): Returns the index of first occurrence. Start and End index are not necessary parameters.

Syntax:

python - Sample - python code :

List.index(element[,start[,end]])
click below button to copy the code. By Python tutorial team

python - Sample - python code :

List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(List.index(2))
click below button to copy the code. By Python tutorial team

python programming - Output :

1

Another example:

python - Sample - python code :

List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(List.index(2,2))
click below button to copy the code. By Python tutorial team

python programming - Output :

4

python - Sample - python code :

List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
 
# will check from index 2 to 3.
print(List.index(2,2,4))
click below button to copy the code. By Python tutorial team

python - Sample - python code :

Traceback (most recent call last):
  File "", line 1, in 
    List.index(2,2,4)
ValueError: tuple.index(x): x not in tuple
click below button to copy the code. By Python tutorial team
  • min() : Calculates minimum of all the elements of List.

Syntax:

python - Sample - python code :

min(List)
click below button to copy the code. By Python tutorial team

python - Sample - python code :

List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
print(min(List))
click below button to copy the code. By Python tutorial team

python programming - Output :

1.054
  • max(): Calculates maximum of all the elements of List.

Syntax:

python - Sample - python code :

max(List)
click below button to copy the code. By Python tutorial team

python - Sample - python code :

List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
print(max(List))
click below button to copy the code. By Python tutorial team

python programming - Output :

5.33

sort() and reverse() functions

  • reverse(): Sort the given data structure (both tuple and list) in ascending order. Key and reverse_flag are not necessary parameter and reverse_flag is set to False, if nothing is passed through sorted().

Syntax:

python - Sample - python code :

sorted([list[,key[,Reverse_Flag]]])
 list.sort([key,[Reverse_flag]])
click below button to copy the code. By Python tutorial team

python - Sample - python code :

List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
 
#Reverse flag is set True
List.sort(reverse=True) 
 
#List.sort().reverse(), reverses the sorted list  
print(List)
click below button to copy the code. By Python tutorial team

python programming - Output :

[5.33, 4.445, 3, 2.5, 2.3, 1.054]

python - Sample - python code :

List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
sorted(List)
print(List)
click below button to copy the code. By Python tutorial team

python programming - Output :

[1.054, 2.3, 2.5, 3, 4.445, 5.33]

Deletion of List Elements

To Delete one or more elements, i.e. remove an element, many built in functions can be used, such as pop() & remove() and keywords such as del.

  • pop(): Index is not a necessary parameter, if not mentioned takes the last index.

Syntax:

python - Sample - python code :

list.pop([index])
click below button to copy the code. By Python tutorial team

Note: Index must be in range of the List, elsewise IndexErrors occurs.

python - Sample - python code :

List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
print(List.pop())
click below button to copy the code. By Python tutorial team

python programming - Output :

2.5

python - Sample - python code :

List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
print(List.pop(0))
click below button to copy the code. By Python tutorial team

python programming - Output :

2.3
  • del() : Element to be deleted is mentioned using list name and index.

Syntax:

python - Sample - python code :

del list.[index]
click below button to copy the code. By Python tutorial team

python - Sample - python code :

List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
del List[0]
print(List)
click below button to copy the code. By Python tutorial team

python programming - Output :

[4.445, 3, 5.33, 1.054, 2.5]
  • remove(): Element to be deleted is mentioned using list name and element.

Syntax:

python - Sample - python code :

list.remove(element)
click below button to copy the code. By Python tutorial team

python - Sample - python code :

List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
List.remove(3)
print(List)
click below button to copy the code. By Python tutorial team

python programming - Output :

[4.445, 5.33, 1.054, 2.5]

Wikitechy tutorial site provides you all the learn python , free python online course , best online course to learn python , learn how to program in python python training free

Related Searches to List methods in Python

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×