Python List Operations – Python Tutorial

1 year ago Lalit Bhagtani 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.

[datacamp_exercise lang=”python” height=”50″ min-height=”50″ max-height=”100″]
[datacamp_sample_code]
# 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)
[/datacamp_sample_code]
[datacamp_solution]

[/datacamp_solution]
[datacamp_sct]

[/datacamp_sct]
[/datacamp_exercise]

List Repetition

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

[datacamp_exercise lang=”python” height=”50″ min-height=”50″ max-height=”100″]
[datacamp_sample_code]
# create list object and assign it to variable ls
ls = [1,2,3,4]

# example of repetition
repetition = ls * 3
print(repetition)
[/datacamp_sample_code]
[datacamp_solution]

[/datacamp_solution]
[datacamp_sct]

[/datacamp_sct]
[/datacamp_exercise]

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.

[datacamp_exercise lang=”python” height=”50″ min-height=”50″ max-height=”100″]
[datacamp_sample_code]
# create list object and assign it to variable ls
ls = [1,2,3,4,5]

# example of contains
contains = 1 in ls
print(contains)
[/datacamp_sample_code]
[datacamp_solution]

[/datacamp_solution]
[datacamp_sct]

[/datacamp_sct]
[/datacamp_exercise]

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.
[datacamp_exercise lang=”python” height=”50″ min-height=”50″ max-height=”100″]
[datacamp_sample_code]
# 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)
[/datacamp_sample_code]
[datacamp_solution]

[/datacamp_solution]
[datacamp_sct]

[/datacamp_sct]
[/datacamp_exercise]

List Length

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

Syntax: len(< List Object >)

For example:
[datacamp_exercise lang=”python” height=”50″ min-height=”50″ max-height=”50″]
[datacamp_pre_exercise_code]

[/datacamp_pre_exercise_code]
[datacamp_sample_code]
# 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)

[/datacamp_sample_code]
[datacamp_solution]

[/datacamp_solution]
[/datacamp_exercise]

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:
[datacamp_exercise lang=”python” height=”50″ min-height=”50″ max-height=”50″]
[datacamp_pre_exercise_code]

[/datacamp_pre_exercise_code]
[datacamp_sample_code]
# 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)
[/datacamp_sample_code]
[datacamp_solution]

[/datacamp_solution]
[/datacamp_exercise]

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