Python Forum
Detecting String Elements in a List within Given Message
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Detecting String Elements in a List within Given Message
#2
Note that example_list is actually a set because its using {} not []
message = 'foo bar hello'
 
example_list = {'foo bar', 'foo'}

 
for item in example_list:
    if message.startswith(item):
        print(item)
Output:
foo foo bar
Reply


Messages In This Thread
RE: Detecting String Elements in a List within Given Message - by Yoriz - Mar-18-2019, 08:11 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 634 Jan-27-2024, 04:03 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 608 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Checking if a string contains all or any elements of a list k1llcod3 1 1,315 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  iterating and detecting the last Skaperen 3 1,216 Oct-01-2022, 05:23 AM
Last Post: Gribouillis
  How to change the datatype of list elements? mHosseinDS86 9 2,221 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  Detecting float or int in a string Clunk_Head 15 5,129 May-26-2022, 11:39 PM
Last Post: Pedroski55
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,517 May-17-2022, 11:38 AM
Last Post: Larz60+
  module detecting if imported vs not Skaperen 1 1,765 Nov-19-2021, 07:43 AM
Last Post: Yoriz
  detecting a generstor passed to a funtion Skaperen 9 3,874 Sep-23-2021, 01:29 AM
Last Post: Skaperen
  Why am I getting list elements < 0 ? Mark17 8 3,386 Aug-26-2021, 09:31 AM
Last Post: naughtyCat

Forum Jump:

User Panel Messages

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