Python Forum
two conditionals with intermediate code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
two conditionals with intermediate code
#4
In this specific case I don't see any downside to just doing the split initially. You can find out if the '=' is present from the result of the split.

l = ["no equals", "this=that"]

for s in l:
    items = s.split('=', maxsplit=1)
    if len(items) > 1:
        # has the equals
        print(f"{items[0]}  ->  {items[1]}")
    else:
        print(f"'{items[0]}' didn't have an equals sign")
And if you wanted to, you could use the walrus operator to combine the split and the if lines.

If split behaved differently than it does and failed when the split character was not present, then I'd probably wrap it in a try/except. I'm not sure if that's useful, or if I've misinterpreted something.
Reply


Messages In This Thread
RE: two conditionals with intermediate code - by bowlofred - Jul-12-2020, 03:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Doubt about conditionals in Python. Carmazum 6 1,669 Apr-01-2023, 12:01 AM
Last Post: Carmazum
  conditionals based on data frame mbrown009 1 926 Aug-12-2022, 08:18 AM
Last Post: Larz60+
  Efficiency with regard to nested conditionals or and statements Mark17 13 3,299 May-06-2022, 05:16 PM
Last Post: Mark17
  Nested conditionals vs conditionals connected by operators dboxall123 8 3,142 Feb-18-2022, 09:34 PM
Last Post: dboxall123
  Nested Conditionals shen123 3 2,673 Jul-28-2021, 08:24 AM
Last Post: Yoriz
  Invalid syntax using conditionals if - else jperezqu 1 2,366 Jan-13-2021, 07:32 PM
Last Post: bowlofred
  conditionals with boolean logic?? ridgerunnersjw 3 2,028 Sep-26-2020, 02:13 PM
Last Post: deanhystad
  Conditionals, while loops, continue, break (PyBite 102) Drone4four 2 3,032 Jun-04-2020, 12:08 PM
Last Post: Drone4four
  Nested Conditionals HELP absolum 4 2,833 Jul-17-2019, 06:40 PM
Last Post: absolum
  intermediate book recommendation please? jameson984 1 2,064 Jul-15-2019, 03:21 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

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