# Check if Two Strings are Anagrams in Python Using sort()

(Last Updated On: 11/10/2023)

Have you ever wondered how to determine if two words are anagrams of each other? An anagram is a word formed by rearranging the letters of another word. It’s a fun and interesting problem to solve, and Python provides us with powerful tools to tackle it. In this blog post, we will explore a simple and efficient Python program to check if two strings are anagrams using inbuilt list & sort() function.

By leveraging the list comprehension and sort() method, we can easily compare the sorted versions of two strings and determine if they contain the same set of characters. So, let’s dive in and discover how to check if two strings are anagrams in Python using sort().

## Python Program to Check if Two Strings are Anagrams Using Inbuilt List & sort() Function

```# Check If Two Strings are Anagram Using Inbuilt List and Sort() Methods in python
#Declare Inputs
inp1 = "listen"
inp2 = "silent"

#Sort Elements
x = [inp1[i] for i in range(0,len(inp1))]
x.sort()
y = [inp2[i] for i in range(0,len(inp2))]
y.sort()

# the sorted strings are checked
if (x == y):print("The strings are anagrams.")
else: print("The strings aren't anagrams.")

## Example 2 for "The strings aren't anagrams."

#Declare Inputs
inp1 = "listen"
inp2 = "silenti"

#Sort Elements
x = [inp1[i] for i in range(0,len(inp1))]
x.sort()
y = [inp2[i] for i in range(0,len(inp2))]
y.sort()

# the sorted strings are checked
if (x == y):print("The strings are anagrams.")
else: print("The strings aren't anagrams.")
```

### Code Explanation

Declarinng Inputs

Two input strings are declared: inp1 and inp2. In the first case, inp1 is set to “listen” and inp2 is set to “silent”.

Sorting Elements

We create a new list x by iterating over the characters of inp1 and adding them to the list. Then we sort the elements in x using the sort() method, which rearranges the elements in ascending order. Next, we create a new list y by iterating over the characters of inp2 and adding them to the list and sort the elements in y using the sort() method.

Checking if the Strings are Anagrams

We compare the sorted lists x and y using the == operator. If the sorted lists are equal, it means that the original strings are anagrams. In this case, print the message “The strings are anagrams.”

If the sorted lists are not equal, it means that the original strings are not anagrams. In this case, print the message “The strings aren’t anagrams.”

Repeating the Process for Another Example

We repeat the same process with a different example to demonstrate the case where the strings are not anagrams. In this case, inp1 is still “listen” but inp2 is changed to “silent”.

Checking if the Strings are Anagrams Again

We sort the elements in x and y as before and compare the sorted lists x and y. Since the sorted lists are not equal, print the message “The strings aren’t anagrams.”

#### Output:

The yield of the code may be a message that demonstrates whether the two input strings are re-arranged words or not. On the off chance that the sorted records of the two strings are break even with, the message “The strings are re-arranged words.” is printed. On the off chance that the sorted records of the two strings are not break even with, the message “The strings aren’t re-arranged words.” is printed.

``````The strings are anagrams.
The strings aren't anagrams.``````

#### Some alternate methods for checking if two strings are anagrams are:

Using sorted() Function:

It can be used to sort the characters of a string and return a new sorted list. To check if two strings are anagrams, you can sort the characters of both strings using sorted() and compare the sorted lists. The advantage of using sorted() is that it directly returns the sorted list without modifying the original string. However, using sorted() requires additional memory to store the sorted lists, and it may not be as efficient as the inbuilt list and sort() method.

Using Counter():

To check if two strings are anagrams, you can create Counter objects for both strings and compare them. The advantage of using Counter is that it counts the occurrences of each character efficiently, without the need for sorting. However, using Counter requires importing the collections module and may not be as easy for beginners.

We prefer the sort() function as it is simple and easy to implement. It leverages the convenience of the built-in sort() method, which sorts the characters of the strings in ascending order. Sorting the characters allows for efficient comparison of the two strings using the equality operator ==. This method provides a clear representation of the logic for checking anagram strings.

### Conclusion

In conclusion, the code demonstrates a simple and efficient approach to check if two strings are anagrams using the inbuilt list and sort() methods in Python. By sorting the characters of each string and comparing the sorted lists, we can determine if the strings contain the same characters in the same quantities, regardless of their original order.

This technique provides an easy way to identify anagrams and can be easily applied to various scenarios where string comparison is required. The code showcases the power of Python’s built-in functions for manipulating lists and highlights the versatility of the language in solving common programming problems. By understanding and utilising these methods, we can efficiently determine whether two strings are anagrams or not.

Q: How does the code sort the elements?

A: The code converts each input string into a list of characters. It then uses the sort() method to arrange the characters in alphabetical order. This ensures that the letters are in the same order for both strings.

Q: Why do we need to sort the elements?

A: Sorting the elements allows us to compare the strings easily. By sorting the characters, we ensure that the same letters in different positions will be treated as equal.

Q: What does the equality operator (==) do?

A: The equality operator (==) compares two values and returns True if they are equal, and False otherwise. In this code, it is used to check if the sorted lists of characters from the strings are the same.

Q: Can I use this code for phrases or sentences?

A: Yes, the code can be used for phrases or sentences as well. It will check if the input strings have the same set of characters, regardless of spaces or punctuation.

Q: Does the code differentiate between uppercase and lowercase letters?

A: Yes, the code is case-sensitive, thus the uppercase and lowercase letters are considered different. For example, “listen” and “Silent” would not be recognized as anagrams.