Click here to Skip to main content
15,666,479 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want a list that has only one character of a given length.

What I have tried:

I tried something like this but it isn't working:

lst = list(input("list: "))
for i, j in lst:
    if len(j) == len(i):

I also tried to convert it into set and then intersect by assigning key as len and then subtracting that set from the original set. But set intersection does't has a key so it isn't working. I did something like this:

lst = set(list(input("list: ")))
to_sub = lst.intersection(lst,key = len)
lst = lst - to_sub

How else can I do it? None of this is working.
Updated 14-Dec-22 5:07am
CPallini 14-Dec-22 10:59am    
Could you please give us an example of input and expected output?

1 solution

How about a dictionary comprehension[^]?
lst = list(input("list: ").strip().split())
lst = list({len(s): s for s in lst}.values())
NB: You need to split the input value unless you want your list to contain an entry per character, with all entries have a length of 1.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900