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 and group similar words with re? cartonics 4 731 Oct-27-2023, 05:36 PM
Last Post: deanhystad
  Program to find Mode of a list PythonBoy 6 1,087 Sep-12-2023, 09:31 AM
Last Post: PythonBoy
  Function to count words in a list up to and including Sam Oldman45 15 6,573 Sep-08-2023, 01:10 PM
Last Post: Pedroski55
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,211 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Working with Excel and Word, Several Questions Regarding Find and Replace Brandon_Pickert 4 1,551 Feb-11-2023, 03:59 PM
Last Post: Brandon_Pickert
  Find (each) element from a list in a file tester_V 3 1,212 Nov-15-2022, 08:40 PM
Last Post: tester_V
  read a text file, find all integers, append to list oldtrafford 12 3,563 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,593 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  find some word in text list file and a bit change to them RolanRoll 3 1,527 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  How to find the second lowest element in the list? Anonymous 3 2,009 May-31-2022, 01:58 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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