Warning: session_start(): open(/tmp/sess_296f8c62ed41e5c7a7d0b093737b7fb2, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Remove Duplicates From A List Of Lists In Python - LearnShareIT

How To Remove Duplicates From A List Of Lists In Python

Remove duplicates from a list of lists in Python

To remove duplicates from a list of lists in Python, you can use two ways: using the sorted() function in combination with the set() function and using the map() function in combination with the set() function. Read the following article for more information.

Remove duplicates from a list of lists in Python

Use the sorted() function

Syntax:

sorted(object, key=None, reverse=False)

Parameters:

  • object: a string, an interpolator, or an iterator object.
  • key: sorted() function with key value, then sort. The default is none.
  • reverse: default value is False. If reverse = True, the list will sort reverse.

The sorted() function in Python sorts the elements of an object in a particular order (ascending or descending) and returns the object as a sorted list.

Example:

  • Initialize a list of arrays where some of the arrays are the same.
  • Use the sorted() function to sort the list.
  • Convert the list of tuples to a set to eliminate duplicates.
  • Note: Remember to convert the nested list to a tuple before converting it to a set because lists are mutable and non-hashable and cannot be a member of a set.
  • Finally, use the list() function to return the results to a list.
# The list consists of sublists
myList = [[0, 0, 1], [1, 1, 0], [1, 1, 0], [1, 2, 3], [4, 5, 6]]

# Use the sorted() and set() function
result = list(set(tuple(sorted(i)) for i in myList))

print("List after removing the same sublists: ", result)

Output:

List after removing the same sublists: [(1, 2, 3), (0, 0, 1), (4, 5, 6), (0, 1, 1)]

Use the map() and set() function

Syntax:

map(func,iterable)

Parameters:

  • func: given function to iterate over the elements of iterable.
  • iterable: the object you want to browse.

Example:

  • Initialize a list of arrays where some of the arrays are the same.
  • The map function converts the list to a tuple object that can be hashed (so that the list is a set member).
  • Use the set() function to remove duplicates of the same.
  • Finally, use the list() function to return the results to a list.
# The list consists of sublists
myList = [[0, 0, 1], [1, 1, 0], [1, 1, 0], [1, 2, 3], [4, 5, 6]]

# Use the map() and set() functions to remove duplicates from a list of lists
result=list(set(map(tuple,myList)))

print("List after removing the same sublists: ", result)

Output:

List after removing the same sublists: [(1, 2, 3), (0, 0, 1), (1, 1, 0), (4, 5, 6)]

Summary

Thank you for being so interested in my article. Hope you got an idea for the topic of how to remove duplicates from a list of lists in Python. You can use both ways for this theme. Let us know if the article is flawed or if you have other ideas.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *