Check if a list is empty in Python

By: On:

In this Python tutorial, we will discuss how to check if a list is empty in Python. We will also check:

  • Check if a list is empty in Python
  • Python size of a list
  • Python return multiple values from a list
  • Remove duplicates from list Python
  • Loop through list python
  • Iterate through a list using range() method in python
  • List Index out of range python
  • Delete an element from list python
  • Python remove last element from the list
  • Compare two lists in python
  • Remove the first element from list python
  • Replace item in list python
  • Python get last element in the list
  • python combines two lists
  • sum of a list in python
  • Max() function in python
  • Min() function in python
  • Count occurrences of a character in list python
  • Check if element exists in list python
  • Remove elements from list python
  • Python uppercase first letter in a list
  • Python unique values in the list
  • Python sort list alphabetically
  • Write a list to file python
  • Count elements in list python
  • Python read file into a list
  • Python list contains a string
  • Python copy elements from one list to another
  • Python function return multiple values
  • Python find an index of all occurrences in a list
  • Python remove multiple items from the list
  • Merge two lists python
  • Python list append to front
  • Flatten a list of lists in python
  • Mean of a list python
  • Python save list to file

If you are new to Python check out the below python tutorials:

Check if a list is empty in Python

The empty list in python is always evaluated to false and non-empty lists evaluate to true which is boolean values. It uses the not operator to determine whether the list is empty or not in python.

Example:

my_list = []
if not my_list:
print('List is empty')
else:
print('List is not empty')

After writing the above code (check if a list is empty in Python), Ones you will print then the output will appear as a “ List is empty ”. Here, the list does not have an element so it is empty. You can refer to the below screenshot for checking if a list is empty in Python.

Check if a list is empty in Python
Check if a list is empty in Python

We can also check the list by using the built-in length function in python. We will use len() method to check if the list is empty.


Example:

my_list = []
if len(my_list)==0:
print('List is empty')
else:
print('List is not empty')

Here, the len() method will return zero if the list is empty. You can refer to the below screenshot for checking if a list is empty in Python.

Check if a list is empty in Python

In this way, we can check if a list is empty in Python.

Python size of a list

To find the size of a list in python, we have the len() method which will give the length of any object.

Example:

list1 = ["Python", "Tutorial", 12]
print("Size of list = ", len(list1))

After writing the above code (Python size of the list), Ones you will print ” len() “ then the output will appear as a “ 3 ”. Here, the len() method will give the size of the element present in the list.

You can refer to the below screenshot for Python size of the list.

Python size of the list
Python size of a list

This way, we can get the length of a list in Python.

Python return multiple values from a list

In Python, multiple values can be returned by the function. By simply using return which holds multiple values separated by commas. Using square bracket [] will give the list.

Example:

def my_list1():
return ['Ani', 20]
result = my_list1()
print(result)

After writing the above code (python return multiple values from a list), Ones you will print ” result “ then the output will appear as a “ [ ‘Ani’, 20 ] ”. Here, the return will give multiple values from the list.

You can refer to the below screenshot for Python size of the list.

Python return multiple values from a list
Python return multiple values from a list

This way, we can get the python return multiple values from a list.

Remove duplicates from list Python

In Python, duplicates elements from a list can be removed by using the list method, count() which will give the number of occurrences of the value and also remove() is used to eliminate the duplicates element present in the list.

Example:

list1 = ["Ankita", "Sid", "Ankita", "Siya", "Sid"]
for v in list1:
if list1.count(v) > 1:
list1.remove(v)
print(list1)

After writing the above code (remove duplicates from list Python), Ones you will print ” list1 “ then the output will appear as a “ [ “Ankita”, “Sid”, “Siya”] ”. Here, the remove() method will remove the duplicate element present in the list.

You can refer to the below screenshot for remove duplicates from the list Python.

Python remove duplicates from list
Remove duplicates from list Python

Also in Python, duplicates elements from the list can be removed by using a dictionary method called fromkeys(). It removes duplicate automatically because keys cannot be duplicate in the dictionary.

Example:

list1 = ["Ankita", "Sid", "Ankita", "Siya", "Sid"]
list1 = list(dict.fromkeys(list1))
print(list1)

After writing the above code (remove duplicates from list Python), Ones you will print ” list1 “ then the output will appear as a “ [ “Ankita”, “Sid”, “Siya”] ”. Here, the fromkeys() method will remove the duplicate element present in the list. You can refer to the below screenshot for remove duplicates from the list Python.

Remove duplicates from list Python

In this way, we can remove duplicates from list Python.

Loop through list python

The loop in python is used to iterate over a list. Iterating over the sequence is called traversal.

Example:

my_value = [2, 4, 6, 8, 10]
for s in my_value:
print(s)

After writing the above code (loop through list python), Ones you will print ” s “ then the output will appear as a “ 2 4 6 8 10 ”. Here,” s “ is the variable that takes the value of the items present inside the sequence and the loop will continue till the last items. You can refer to the below screenshot for loop through list python.

Loop through list python
Loop through list python

This is how we can loop through a list in Python.

Iterate through a list using range() method in python

In python, the range() method is used to returns the sequence of integers and it will iterate through a list in python.

Example:

my_list = [30, 15, 45, 93, 38]
for a in range(len(my_list)):
print(my_list[a])

After writing the above code (iterate through a list using range() method in python), Ones you will print ” my_list[a] “ then the output will appear as a “ 30, 15, 45, 93, 38 ”. Here, the” range() “ method is used with the loop to iterate over the list. You can refer to the below screenshot for iterate through a list using range() method in python.

Iterate through a list using range() method in python
Iterate through a list using range() method in python

This is how we can iterate through a list using range() method in python.

List Index out of range python

In Python, we can access any elements of list by its index, if we will give the index which is not present in the list then, it will give an error message which is list index out of range.

Example:

place = ['Delhi', 'Bangalore', 'Jaipur', 'Pune']
print(place[4])

After writing the above code, Ones you will print ” place[4] “ then the output will appear as a “ list index out of range ”. Here,” place[4] “ is not present so, it will give an error message because index 4 is not on the list. You can refer to the below screenshot for list Index out of range python.

List index out of range in python
List Index out of range python

So, the above error can be solved if the given index is present in the list.

Example:

place = ['Delhi', 'Bangalore', 'Jaipur', 'Pune']
print(place[2])

Here, we will print “place[2]” which is present in the list and the output will be ” Jaipur “, this error is resolved by taking the index in the range. You can refer to the below screenshot.

List Index out of range python

This way, we can fix error List Index out of range python.

Delete an element from list python

In python, the delete is used to remove the particular elements from the list. It will delete the specified element from the list and it will return the rest.

Example:

number = [10, 30, 50, 70, 90]
del number[2]
print(number)

After writing the above code, Ones you will print ” number “ then the output will appear as a “ [10, 30, 70, 90] ”. Here, it will delete the index 2 elements and it will return the rest element. You can refer to the below screenshot for delete an element from the list python.

Delete an element from list python
Delete an element from list python

This is how we can delete an element from list in python.

Python remove last element from the list

In python, to remove the last element from the list, we need to specify the index of the last element and it will remove the element from the list. Here the last index is ” -1 “.

Example:

roll = [11, 22, 33, 44, 55]
my_list = roll[:-1]
print(my_list)

After writing the above code, Ones you will print ” my_list “ then the output will appear as a “ [11, 22, 33, 44] ”. Here, it will remove the last elements from the list which is ” 55 “ and it will return the rest element. You can refer to the below screenshot to remove the last element from the list in python.

Python remove last element from the list
Python remove last element from the list

This is how we can remove last element from the list in python.

Compare two lists in python

In python, to compare two list we need to compare the two list elements and if the element matches then append that element in the new variable.

Example:

list1 = [22, 32, 45, 56, 60]
list2 = [60, 45, 55, 34, 22, 35]
list3 = []
for number in list1:
if number in list2:
if number not in list3:
list3.append(number)
print(list3)

After writing the above code, Ones you will print ” list3 “ then the output will appear as a “ [22, 45, 60] ”. Here, we have two lists and it will compare the list if the element matches then it will get appended to the “list3”. You can refer to the below screenshot for compare two lists in python.

Compare two lists in python
Compare two lists in python

This is how we can compare two lists in python.

Remove the first element from list python

In python, the remove() method is used to remove the matching element from the list. It will search for the given first element in the list, If the given element matches then it is removed.

Example:

my_list = [30, 15, 45, 93, 38]
my_list.remove(30)
print(my_list)

After writing the above code, Ones you will print ” my_list “ then the output will appear as a “ [ 15, 45, 93, 38] ”. Here, the remove() method will remove ” 30 ” which is the first element from the list. You can refer to the below screenshot to remove the first element from the list python.

Remove the first element from list python
Remove the first element from list python

This is how we can remove the first element from list python.

Replace item in list python

In python, to replace item in the list, we have to mention the index number with the value to replace it with and it will give a list with replaced item.

Example:

my_list = [20, 34, 39, 'Apple', 'Mango', 'Orange']
my_list[1] = 55
print(my_list)

After writing the above code, Ones you will print ” my_list “ then the output will appear as a “ [ 20, 55, 39, ‘Apple’, ‘Mango’, ‘Orange’ ] ”. Here, the index 1 value will be replaced with 55 and the result will be a list. You can refer to the below screenshot to replace the item in the list python.

Replace item in list python
Replace item in list python

This is how we can replace item in list python.

Python get last element in the list

In python, to get the last element from the list, we can use the negative index as we want the last element from the list so, the count will start from the end by using -1 as an index.

Example:

my_list = [10,22,30,43,55]
last_item = my_list[-1]
print('Last Element: ', last_item)

After writing the above code, Ones you will print ” last_item “ then the output will appear as a “ 55 ”. Here, you will get the last element which is 55, and -1 is the index from the last. You can refer to the below screenshot python get the last element in the list.

Python get last element in the list
Python get last element in the list

This is how we can get last element in the list python.

Python combines two lists

In python, to combine two lists in python, we can simply use ” + “ operator to combine two list.

Example:

my_list1 = [2, 4, 5]
my_list2 = [7, 8, 9, 10]
combine = my_list1 + my_list2
print(combine)

After writing the above code, Ones you will print ” combine “ then the output will appear as a “ [2, 4, 5, 7, 8, 9, 10] ”. Here, by using ” + ” operator the two list element is combined and the result will be a single list with all elements. You can refer to the below screenshot python combines two lists.

Python combines two lists
Python combines two lists

This is how we can combine two lists in python.

Sum of a list in python

In python, we have sum() method to add all the elements from the list.

Example:

my_list1 = [ 2, 4, 5, 7, 8, 9, 10]
my_list2 = sum(my_list1)
print(my_list2)

After writing the above code, Ones you will print ” my_list2 “ then the output will appear as a “ 45 ”. Here, by using the sum() method the elements present in the list will get added and it will give the sum as a result. You can refer to the below screenshot sum of a list in python.

Sum of a list in python
Sum of a list in python

This is how we can do sum of a list in python

Max() function in python

In python, we have a built-in function called max(), it will return the item with the highest value present in the list.

Example:

my_list = [ 2, 4, 52, 7, 88, 99, 10]
value = max(my_list)
print(value)

After writing the above code, Ones you will print ” value “ then the output will appear as a “ 99 ”. Here, by using the max() function, it will return the largest elements present in the list. You can refer to the below screenshot max() function in python.

Max() function in python
Max() function in python

This is how we can do Max() function in python.

Min() function in python

In python, we have a built-in function called min() function, it will return the item with the smallest value present in the list.

Example:

my_list = [ 2, 4, 52, 7, 88, 99, 10]
value = min(my_list)
print(value)

After writing the above code, Ones you will print ” value “ then the output will appear as a “ 2 ”. Here, by using the min() function, it will return the smallest elements present in the list. You can refer to the below screenshot min() function in python.

Min() function in python
Min() function in python

This is how we can do Min() function in python.

Count occurrences of a character in list python

In python, to count the occurrences of a character we can use list.count() to count the multiple occurrence of the specified value present in the list.

Example:

my_list = ['u', 'v', 'u', 'x', 'z']
value = my_list.count('u')
print('Occurrences of character is: ',value)

After writing the above code, Ones you will print ” value “ then the output will appear as a “ 2 ”. Here, by using my_list.count(‘u’) we will get the number of occurrences of the value ‘u’ from the list. You can refer to the below screenshot count occurrences of a character in the list python.

Count occurrences of a character in list python
Count occurrences of a character in list python

This is how we can Count occurrences of a character in list python

Check if element exists in list python

In python, to check if element exists in list python then we can use ” in “ operation to check if element is present in the list or not, if the element is present then the condition holds true and if not then false.

Example:

my_list = ['Tom', 'Jack', 'Harry', 'Edyona']
if 'Jack' in my_list:
print("Yes, 'Jack' is present in list")

After writing the above code, Ones you will print then the output will appear as a “ Yes, ‘Jack’ is present in list ”. Here, by using ” in ” operator with if condition we can find the elements present in the list or not. You can refer to the below screenshot check if the element exists in the list python.

Check if element exists in list python
Check if element exists in list python

This is how we can check if element exists in list python

Remove elements from list python

In python, to remove the element from the list we can use remove() method with argument to remove the specified element from the list.

Example:

my_list = ['Tom', 'Jack', 'Harry', 'Edyona']
my_list.remove('Jack')
print('Updated list: ', my_list)

After writing the above code, Ones you will print ” my_list “ then the output will appear as a “ [‘Tom’, ‘Harry’, ‘Edyona’] ”. Here, the remove() method will remove ” Jack “ from the list. You can refer to the below screenshot to remove elements from the list python.

Remove elements from list python

This is how we can remove elements from list python

Python uppercase first letter in a list

In python, to the uppercase first letter in a list, we will use capitalize() method which will only capitalize the first letter of the word given in the list.

Example:

my_list = ['fivestar', 'gems', 'kitkat']
capital = my_list[0].capitalize()
print(capital)

After writing the above code, Ones you will print ” capital “ then the output will appear as a “ Fivestar ”. Here, the capitalize() method will make the first letter ” Fivestar “ capital because the index is ” [0] “. You can refer to the below screenshot python uppercase first letter in a list.

Python uppercase first letter in a list

This is how we can do python uppercase first letter in a list

Python unique values in the list

In python, to get the unique value in the list we can use the set() method, it gets converted to set with a single copy of duplicates, and then we have to convert it back to list.

Example:

my_list1 = [50, 30, 20, 60, 30, 50]
my_set = set(my_list1)
my_list2 = list(my_set)
print("Unique number is: ",my_list2)

After writing the above code (python unique values in the list), Ones you will print ” my_list2 “ then the output will appear as a “ [50, 20, 30, 60] ”. Here, the set() method will remove the duplicate element present in it, and again we have to convert it to list. You can refer to the below screenshot for python unique values in the list.

Python unique values in the list

This is how we can do python unique values in the list.

Python sort list alphabetically

In python, to sort the list alphabetically we will use sorted() function so that the items are ordered in the position of the letters. Uppercase letter comes before the lowercase letters alphabet.

Example:

my_list = ['ahana', 'Aarushi', 'Vivek', 'john']
to_sort = sorted(my_list)
print(to_sort)

After writing the above code (python sort list alphabetically), Ones you will print ” to_sort “ then the output will appear as a “ [‘Aarushi’, ‘Vivek’, ‘ahana’, ‘john’] ”. Here, the sorted() function will sort the items in alphabetical order that’s why uppercase are before lowercase in the output list. You can refer to the below screenshot for the python sort list alphabetically.

Python sort list alphabetically

This is how we can do python sort list alphabetically

Write a list to file python

In python, we can write a list to file by printing the content of a list to file and the list items will be added to the new line in the output file. The print commands write the element to the opened file.

Example:

My_list1 = ['welcome', 'to', 'python']
My_file = open('output.txt', 'w')
for value in My_list1:
print(My_file, value)
My_file.close()

After writing the above code (write a list to file python), Ones you will print ” (My_file, value) “ then the output will appear. Here, the list item has been written line by line in the output file, the loop is used to iterates at each element in the list. You can refer to the below screenshot for the python sort list alphabetically.

Write a list to file python

This is how we can write a list to file python

Count elements in list python

In python, to count the total elements in the list we can use len() function as it returns the total element present in the list.

Example:

My_list = [10, 11, 12, 13, 14, 15]
element = len(My_list)
print(element)

After writing the above code, Ones you will print ” element “ then the output will appear as a “ 6 ”. Here, by using len() function we will get the total number of elements present in the list. You can refer to the below screenshot to count elements in the list python.

Count elements in list python

This is how we can count elements in list python

Python read file into a list

In python, to read the file into a list we will first open the file with the open() method which will take the filepath an argument and it will read the file, the splitlines will strip all the characters and it will give the list.

Example:

my_file = open("out.txt")
my_line = my_file.read().splitlines()
my_file.close()
print(my_line)

After writing the above code (python read file into a list), Ones you will print ” (my_line) “ then the output will appear. Here, it will read the file and split the lines, and then it will return a list containing the lines of file. You can refer to the below screenshot for python read file into a list.

Python read file into a list

This is how we can read file into a list in python

Python list contains a string

In python, to find whether the list contains a string we use in the operator to check if the list contains the given string or not.

Example:

my_list = ['S', 'W', 'I', 'F', 'T']
if 'F' in my_list:
print('F is present in the list')
else:
print('F is not in the list')

After writing the above code (python list contains a string), Ones you will print then the output will appear. Here, the in operator will check if the string is present in list or not. You can refer to the below screenshot for the python list contains a string.

Python list contains a string

This is how we can find python list contains a string

Python copy elements from one list to another

In python, we can use built-in copy() method to copy the elements from one list to another.

Example:

first_list = []
second_list = [5, 8, 10, 18]
first_list = second_list.copy()
print(first_list)

After writing the above code (python copy elements from one list to another), Ones you will print ” first_list” then the output will appear as ” [5, 8, 10, 18] “. Here, the copy() method is used to copy the elements as it takes reference from the second list to the first list. You can refer to the below screenshot for the python copy elements from one list to another.

Python copy elements from one list to another

This is how we can copy elements from one list to another in python

Python function return multiple values

In python, the function can return multiple values and all values are stored in the variable, a function can return two or more values.

Example:

def function():
str = "Python Guides"
i = 13098
return [str, i];
my_list = function()
print(my_list)

After writing the above code (python function return multiple values), Ones you will print “(my_list)” then the output will appear as “[‘Python Guides’, 13098]”. Here, the function will return the multiple values. You can refer to the below screenshot for the python function to return multiple values.

Python function return multiple values

This is how python function return multiple values

Python find an index of all occurrences in a list

In python, for finding an index of all occurrences of an element in a list we will use for-loop to iterate on each element in the list and append its index to the empty list.

Example:

my_list = [11, 12, 13, 11]
index = []
for x in range(len(my_list)):
if my_list[x] == 11:
index.append(x)
print(index)

After writing the above code (python find an index of all occurrences in a list), Ones you will print “index” then the output will appear as “[0, 3]”. Here, the loop will iterate and all the occurrence of the desired element append its index to an empty list. You can refer to the below screenshot for the python to find an index of all occurrences in a list.

Python find an index of all occurrences in a list

This is how we can find an index of all occurrences in a list python.

Python remove multiple items from the list

In python, to remove multiple items from the list we will use for-loop to iterate through the list and also a list.append(object) to add the object in an empty list.

Example:

my_list = [12, 13, 14, 15]
remove_element = [13, 15]
new_list = []
for element in my_list:
if element not in remove_element:
new_list.append(element)
print(new_list)

After writing the above code (python remove multiple items from the list), Ones you will print “new_list” then the output will appear as “[12, 14]”. Here, the for loop will iterate through the list and it will remove [13, 14] from the new list. You can refer to the below screenshot for the python to remove multiple items from the list.

Python remove multiple items from the list

Merge two lists python

In python, to merge two lists we will use the extend() method, to merge one list to another list in python.

Example:

my_list1 = [12, 13, 14, 15]
my_list2 = ["x", "y", "z"]
my_list1.extend(my_list2)
print(my_list1)

After writing the above code (python merge two lists python), Ones you will print “my_list1” then the output will appear as “[12, 13, 14, 15, ‘x’, ‘y’, ‘z’]”. Here, the extend() method will merge the two list. You can refer to the below screenshot python merge two lists.

Merge two lists python

Python list append to front

In python, to append the element to the font of the list we will use list.insert() with “0” as an index and the specified object to insert at the front of the list.

Example:

my_list = ['y', 'z']
my_list.insert(0, 'x')
print(my_list)

After writing the above code (python list append to front), Ones you will print “my_list” then the output will appear as “[‘x’, ‘y’, ‘z’]”. Here, list.insert() will append the specified element in the front of the list. You can refer to the below screenshot python list append to front.

Python list append to front

Flatten a list of lists in python

Flattening a list of lists merges all the sublists into one list. List comprehensions are one of the ways which will use.

Example:

my_list = [[1,2], [3], [4,5,6]]
new_list = [item for items in my_list for item in items]
print(new_list)

After writing the above code (flatten a list of lists in python), Ones you will print “new_list” then the output will appear as “[1, 2, 3, 4, 5, 6]”. Here, the list comprehension iterates over every list, and each value is added in the main list which gets printed. You can refer to the below screenshot for flattening a list of lists in python.

Flatten a list of lists in python

Mean of a list python

The mean of a list can be calculated by using the sum() and len() function for the list. The sum() will return all the value of the list which will be divide by the number of elements returned by len().

Example:

def Avg(list):
return sum(list)/len(list)
list = [5, 10, 15, 20]
a = Avg(list)
print("Average of the list =",round(a,2))

After writing the above code (mean of a list in python), Ones you will print then the output will appear as “Average of the list = 12.5”. Here, the value of the list will be added and it gets divided by the total number. You can refer to the below screenshot for the mean of a list in python.

Mean of a list python

Python save list to file

In python, for saving list to file we will use write method and for loop to iterate the list.

Example:

fruits = ['Apple', 'Avocado', 'Banana', 'Blueberries']
with open('lfile.txt', 'w') as filehandle:
    for items in fruits:
        filehandle.write('%s\n' % items)

After writing the above code (python save list to file) first, we have to open the file where the list item will be saved. Here, the “w” write method is used and the loop to iterate the items. After running the program the “lfile.txt” will contain the list items. You can refer to the below screenshot for the python save list to file

Python save list to file
Python save list to file

Output:

Python save list to file
Python save list to file

You may like the following Python tutorials:

In this tutorial, we learned how to check if a list is empty in Python.

  • Check if a list is empty in Python
  • get size of a list in Python
  • Python return multiple values from a list
  • Remove duplicates from list Python
  • Loop through list python
  • Iterate through a list using range() method in python
  • List Index out of range python
  • Delete element from list python
  • Python remove last element from the list
  • Compare two lists in python
  • Remove the first element from list python
  • Replace item in list python
  • Python get last element in the list
  • python combines two lists
  • sum of a list in python
  • Max() function in python
  • Min() function in python
  • Count occurrences of a character in list python
  • Check if element exists in list python
  • Remove elements from list python
  • Python uppercase first letter in a list
  • Python unique values in the list
  • Python sort list alphabetically
  • Write a list to file python
  • Count elements in list python
  • Python read file into a list
  • Python list contains a string
  • Python copy elements from one list to another
  • Python function return multiple values
  • Python find an index of all occurrences in a list
  • Python remove multiple items from the list
  • Merge two lists python
  • Python list append to front
  • Flatten a list of lists in python
  • Mean of a list python
  • Python save list to file

Leave a Comment