Python Forum
Trying to find first 2 letter word in a list of words
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to find first 2 letter word in a list of words
#1
Hello

I am trying to get this function working but cannot work out what is wrong. Any help will be gratefully received. My input is:

xs = ["this", "is", "a", "dead", "parrot"]

def find_first_2_letter_word(xs):
    for wd in xs:
        if len(wd) == 2:
            return wd
        return " "
    
print("The first 2 letter word is ", find_first_2_letter_word(xs))
My output is:
Output:
Python 3.8.4 (tags/v3.8.4:dfa645a, Jul 13 2020, 16:46:45) [MSC v.1924 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license()" for more information. >>> ================== RESTART: C:\Users\John\Desktop\testing1.py ================== The first 2 letter word is >>>
As can be seen the program prints the statement but fails to show the first 2 letter word.

Thank you
Reply


Messages In This Thread
Trying to find first 2 letter word in a list of words - by Oldman45 - Aug-08-2020, 08:34 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Find Common Elements in 2 list quest 4 109 Apr-14-2021, 03:57 PM
Last Post: quest
  Function to count words in a list up to and including Sam Oldman45 13 607 Feb-18-2021, 10:22 AM
Last Post: perfringo
  Trying to get the first letter of every word in a list DanielCook 2 299 Jan-05-2021, 05:06 PM
Last Post: deanhystad
  List of error codes to find (and count) in all files in a directory tester_V 8 515 Dec-11-2020, 07:07 PM
Last Post: tester_V
  Find _ in a list 1234 15 832 Dec-01-2020, 07:53 PM
Last Post: bowlofred
  how to check if string contains ALL words from the list? zarize 6 803 Jul-22-2020, 07:04 PM
Last Post: zarize
  How to find the first and last of one of several characters in a list of strings? tadsss 2 544 Jun-02-2020, 05:23 PM
Last Post: bowlofred
  Find index value in List Martin2998 3 713 May-12-2020, 02:17 PM
Last Post: deanhystad
  item from a line to list however when i print the line instead of words i get letters Sutsro 5 692 Apr-22-2020, 02:39 PM
Last Post: deanhystad
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 580 Apr-22-2020, 01:01 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020