Ask Your Question
0

How can I retrieve identical items from a list regardless of letter case?

asked 2021-07-30 11:00:00 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-01-21 04:00:00 +0000

ladyg gravatar image

You can convert each item in the list to lowercase using the lower() method and then use a set() to remove duplicates while ignoring case. Here's an example code:

my_list = ["apple", "Banana", "ORANGE", "banana", "grape", "GRAPE"]
unique_items = set(map(str.lower, my_list))

print(unique_items)  # {'apple', 'banana', 'grape', 'orange'}

In this code, the map() function is used to apply the lower() method to each item in the list. The resulting lowercase items are passed to the set() to remove duplicates. The output shows the unique items in the list without regard to letter case.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-07-30 11:00:00 +0000

Seen: 8 times

Last updated: Jan 21 '22