-3

I am new to programming and I am trying to figure out this problem. I have a list of elements and I want to find words that have the same length. This is what I've tried:

list1 = ["dog","cat","people","tank","pop","joop","count"]    
list3 = []

for i in range(len(list1)):   
    for j in range(len(list1):    
        if len(list1[i]) == len(list1[j]):    
            list3.append(list[i])
return list3

I want to return list3 = [ "dog","cat","joop","tank"] because each word in this last has the same length as at least one other word in the list.

Woody1193
  • 7,252
  • 5
  • 40
  • 90
BBC8889
  • 9
  • 2
  • 2
    What if you have two 3-letter words and two 4-letter words. They each match the other word in the pair, but not the other 2 words. – Barmar Jan 20 '23 at 00:08
  • 5
    because when `i == j` your are checking every word against itself. – Julien Jan 20 '23 at 00:08
  • How should it even be returned, a list? and as @Barmar said what if there is two length that are repeated? –  Jan 20 '23 at 00:12
  • @CoolGuy I want to return a list with with the following list3 = ["dog,"cat"] since they are same length if I had a list that had list1 = ["bat","cat","dog"] I would return a list that contained "bat" "cat" and "dog" – BBC8889 Jan 20 '23 at 00:12
  • 1
    @CoolGuy They seem to be talking about collecting them into `list3` – Barmar Jan 20 '23 at 00:13
  • is this how it should be `['dog, dog', 'cat, dog', 'dog, cat', 'cat, cat', 'people, people', 'tank, tank']`? Please create a example list of how it should look like. –  Jan 20 '23 at 00:13
  • 1
    It's better to show some more inputs - with 3- and 4-letter words matching. – Daniel Hao Jan 20 '23 at 00:14
  • 1
    as Julian noted, you need to not compare lengths when i == j. and as Barmar noted, you can have multiple groups of words of the same length. You could create a dictionary , rather than a list which would hold a list for each length match you get. – Jim Robinson Jan 20 '23 at 00:15
  • Why shouldn't the output have `pop` in it? Isn't that the same length as `dog` and `cat`? – Karl Knechtel Jan 20 '23 at 01:21

4 Answers4

0

If we understand your question by now, you do want to group all same-size words? IF so, then you could try this defaultdict from collections module:

If this is not what you expect, then please make the goal clear.


L = ['dog', 'cat', 'bike', 'book', 'packet']

from collections import defaultdict

ddc = defaultdict(list)

for item in L:
    size = len(item)          # find the size of each word
    ddc[size].append(item)    # then group them together by size

    
print(ddc)
defaultdict(<class 'list'>, {3: ['dog', 'cat'], 4: ['bike', 'book'], 6: ['packet']})
Daniel Hao
  • 4,922
  • 3
  • 10
  • 23
  • I want to return a list with the values ["dog","cat","bike","book"]. The reason I need a list is because I have to do additional manipulation once I find words of the same length – BBC8889 Jan 20 '23 at 00:23
  • IT's very easy to make this dict to the list. Can you try first at least? and come back with question. – Daniel Hao Jan 20 '23 at 00:24
0

My advice for situations like this is to use a map rather than using a list:

list1 = ["dog","cat","people","tank","pop","joop","count"]     
results = {}
for item in list1:
    length = len(item)
    if length in results:
        results[length].append(item)
    else:
        results[length] = [item]

print(results) # {3: ['dog', 'cat', 'pop'], 6: ['people'], 4: ['tank', 'joop'], 5: ['count']}

This works by iterating over all words in list1, getting the number of characters in each and adding it to the dictionary entry associated with that length. You can then filter that dictionary for entries that contain more than one word:

import itertools
filtered = list(itertools.chain(*filter(lambda v: len(v) > 1, results.values())))
print(filtered) # ['dog', 'cat', 'pop', 'tank', 'joop']

This code works by calling the filter function with a lambda that checks if the value associated with each key (length) has more than one value, and then chaining these sub-lists together into a single list, which is returned. For more information on how itertools.chain works and why I included the *, see this answer.

Woody1193
  • 7,252
  • 5
  • 40
  • 90
0

Another solution would be to do something like this:

def find_multiples(words):
  lengths = [len(word) for word in words]
  multiples = [word for word in words if lengths.count(len(word)) > 1]
  return multiples

After doing some testing on this, the average time for running this function 10 times was 1.629 seconds, if speed is something you would like, and it is fairly compact.

-1

If it is possible the same word appears multiple times, you might choose this solution:

def tryit():
    list1 = ["dog","cat","people","tank","pop","joop","count"]    
    mydict = {}
    l = len(list1)
    for i in range(l):
        vi = list1[i]
        li = len(vi)
        for j in range(l):
            vj=list1[j]
            lj = len(vj)   
            if li == lj and i != j :
                if li in mydict:
                    additem(mydict[li],vi)
                    additem(mydict[li],vj)
                else:        
                    mydict[li] = [vi,vj]
    return mydict

def additem(list,v):
    if v in list:
        return # don't place duplicates in list
    else:
        list.append(v)
    # This depends on the fact that list is mutable, so 
    # the passed list is updated. 
    
if __name__ == '__main__':
    print(tryit())    enter code here

Result: {3: ['dog', 'cat', 'pop'], 4: ['tank', 'joop']}

Jim Robinson
  • 189
  • 1
  • 10