Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
In Python, you can easily compare two strings or two lists using comparison operators such as ==
, !=
, <
, >
, <=
, and >=
. Let's see how to compare strings and lists in Python.
You can compare two strings using the ==
(equality) operator to check if they are equal or the !=
(inequality) operator to check if they are not equal. Python compares strings lexicographically, which is similar to alphabetical order but takes into account all characters' Unicode code points.
str1 = "hello" str2 = "world" str3 = "hello" print(str1 == str2) # Output: False print(str1 == str3) # Output: True print(str1 != str2) # Output: True
You can also use other comparison operators like <
, >
, <=
, >=
to compare strings lexicographically.
print(str1 < str2) # Output: True print(str1 > str2) # Output: False
Similar to strings, you can compare two lists using the ==
(equality) operator to check if they are equal or the !=
(inequality) operator to check if they are not equal. Lists are compared element-wise, in the same order.
list1 = [1, 2, 3] list2 = [1, 2, 3] list3 = [1, 2, 4] print(list1 == list2) # Output: True print(list1 == list3) # Output: False print(list1 != list3) # Output: True
Again, you can use other comparison operators like <
, >
, <=
, >=
to compare lists lexicographically.
list4 = [1, 2] list5 = [1, 2, 3] print(list4 < list5) # Output: True print(list4 > list5) # Output: False
Note that when comparing lists or strings, both types must be the same. Comparing a string to a list will result in a TypeError
.
Python compare two strings:
str1 = "Hello" str2 = "World" result = str1 == str2 print(result) # False
String comparison operators in Python:
str1 = "Hello" str2 = "World" result = str1 < str2 print(result) # True (lexicographical order)
Case-insensitive string comparison in Python:
str1 = "hello" str2 = "HELLO" result = str1.lower() == str2.lower() print(result) # True
Levenshtein distance for string similarity in Python:
(Requires the python-Levenshtein
library)
import Levenshtein str1 = "kitten" str2 = "sitting" distance = Levenshtein.distance(str1, str2) similarity = 1 - distance / max(len(str1), len(str2)) print(similarity) # 0.5714285714285714
Comparing two strings with difflib in Python:
import difflib str1 = "Hello" str2 = "World" d = difflib.Differ() diff = list(d.compare(str1, str2)) print(diff) # Output: [' H', ' e', ' l', ' l', ' o', '- W', '- o', '- r', '- l', '- d']
Fuzzy string matching in Python:
(Requires the fuzzywuzzy
library)
from fuzzywuzzy import fuzz str1 = "Hello" str2 = "World" similarity_ratio = fuzz.ratio(str1, str2) print(similarity_ratio) # 20
Compare Unicode strings in Python:
str1 = "���" str2 = "���" result = str1 == str2 print(result) # True
Python compare two lists:
list1 = [1, 2, 3] list2 = [4, 5, 6] result = list1 == list2 print(result) # False
Element-wise list comparison in Python:
list1 = [1, 2, 3] list2 = [4, 5, 6] result = all(x == y for x, y in zip(list1, list2)) print(result) # False
Case-insensitive list comparison in Python:
list1 = ["apple", "Banana", "Cherry"] list2 = ["Apple", "banana", "cherry"] result = all(x.lower() == y.lower() for x, y in zip(list1, list2)) print(result) # True
Check if lists have common elements in Python:
list1 = [1, 2, 3] list2 = [3, 4, 5] result = any(x in list2 for x in list1) print(result) # True
Find differences between two lists in Python:
list1 = [1, 2, 3] list2 = [3, 4, 5] diff = list(set(list1) - set(list2)) print(diff) # [1, 2]
Comparing nested lists in Python:
nested_list1 = [[1, 2], [3, 4]] nested_list2 = [[1, 2], [3, 4]] result = nested_list1 == nested_list2 print(result) # True