Home > Blockchain >  How can I use match/case from Python3 to build cases for specific list index positions?
How can I use match/case from Python3 to build cases for specific list index positions?

Time:01-26

I have a list that will look like [True, False, False, False, True, ...]

This list will always have a preset length and I want to use match case syntax instead of if else statement to determine what is the value for every index

Here is what I had in mind:

    # The returned result is a list of True or False -> [True, True, False, True, False] etc.
    match list_items:
        case list_items[0] == 'True'
        case list_items[1] == 'False'

Any help appreciated, thanks!

CodePudding user response:

Your question is ambiguous, but I think this is what you're asking for. Also, I agree with @JonSG that an alternate approach may be the best idea as this is clunky and weird, but here you go. I would use enumerate to accomplish this. Here's a glimpse into how enumerate works:

>>> mylist = ["rock","paper","scissors"]
>>> for new_tuple in enumerate(mylist):
...   print(new_tuple)
...
(0, 'rock')
(1, 'paper')
(2, 'scissors')

So back to my interpretation of your question. I believe you're asking for something like this pseudocode:

for each item in mylist:
   case item0:
      if item0 is True:
         something
      else:
         something else  
   case item1:
      if item1 is True:
         ... keep going...

Now in Python using enumerate:

data = [True, False, False, False, True]
for idx,value in enumerate(data):
    match idx:
        case 0:
            if value:
                print("0: True")
            else:
                print("0: False")
        case 1:
            if value:
                print("1: True")
            else:
                print("1: False")
        case 2:
            if value:
                print("2: True")
            else:
                print("2: False")
        case 3:
            if value:
                print("3: True")
            else:
                print("3: False")
        case 4:
            if value:
                print("4: True")
            else:
                print("4: False")     
# Output:
# 0: True
# 1: False
# 2: False
# 3: False
# 4: True                                                           

CodePudding user response:

The question is somewhat open to interpretation but this is what I think OP is looking for:

data = [True, False, False, False, True]

for index, element in enumerate(data):
    match element:
        case True:
            print(f'True at index {index}')
        case False:
            print(f'False at index {index}')
        case _:
            print(f'Unexpected value at index {index}')

Output:

True at index 0
False at index 1
False at index 2
False at index 3
True at index 4

CodePudding user response:

You simply need to wrap the match statement in a loop, then it will work as expected. If you dont do that it will evaluate the whole list object and a list can't be True or False. You can think of it like (list == True). This statement always evaluates to false and if you use it in an if or match statement it won't work.

This is what you need to do:

list = [True, False, False, False, True]

for element in list:
    match element:
            case True: print(1)
            case False: print(0)

In this example I am simply printing 1 if the value of the index is True and 0 if it is False. The final output is 1 0 0 0 1.

  • Related