Python List Operations – Python Tutorial

2 weeks ago codedestine 0

Python List Operations : In this tutorial, we will learn about different python list operations, some of them are as follows :

  1. List Concatenation
  2. List Repetition
  3. List Contains
  4. List Not Contains
  5. List Length
  6. List Min & Max

Python List Operations

List Concatenation

Addition ( + ) operator can be used to join two list together to create a new list.

# create first list object and assign it to variable f f = [1,2,3,4,5] # create second list object and assign it to variable s s = [6,7,8,9,10] # example of concatenation concatenate = f + s print(concatenate)

List Repetition

Multiplication ( * ) operator can be used to join same list multiple times to create a new list.

# create list object and assign it to variable ls ls = [1,2,3,4] # example of repetition repetition = ls * 3 print(repetition)

List Contains

Operator in can be used to check, if a given element is present in the list or not. If a element is present in the list then return True otherwise return False.

# create list object and assign it to variable ls ls = [1,2,3,4,5] # example of contains contains = 1 in ls print(contains)

List Not Contains

Operator not in can be used to check, if a given element is present in the list or not. If a element is present in the list then return False otherwise return True.

# create list object and assign it to variable ls ls = [1,2,3,4,5] # example of not contains not_contain = 1 not in ls print(not_contain)

List Length

Python’s len() function can be used to get the length of a list.

Syntax: len(< List Object >)

For example:

# create list object and assign it to variable ls ls = [1,2,3,4,5] # call len() method to get length of the list ls length = len(ls) # print the value of variable length print(length)

List Min & Max

Python’s min() and max()  function can be used to get minimum and maximum element in the list.

Syntax: min(< List Object >)

               max(< List Object >)

For example:

# create list object and assign it to variable ls ls = [1,2,3,4,5] # call min() method to get minimum element in the list ls minimum = min(ls) # print the value of variable minimum print(minimum) # call max() method to get maximum element in the list ls maximum = max(ls) # print the value of variable maximum print(maximum)

References :-

  1. List Methods Docs

That’s all for Python List Operations. If you liked it, please share your thoughts in comments section and share it with others too.

Previous