Home > Mobile >  Design decision to avoid having Python merge strings in a list
Design decision to avoid having Python merge strings in a list

Time:03-01

Suppose I have the following list:

a = ["hello",
     "hi",
     "bye"]

I move the elements around, and, by mistake, I end up with:

a = ["hello",
     "bye"
     "hi"]

which is a list of 2 elements: "hello" and "byehi".

  1. How can I have Python detect this mistake?

    Currently I ended up with:

    assert(len(a)==3)
    

    which of course requires update whenever I add an element to the list.

  2. How to decrease the possibility of this mistake (of course other than being careful)?

    Is there some other separator for lists that won't merge strings that way, for example?

    Is there a linter or an external tool that can detect that?

CodePudding user response:

You can't, really, short of good unit tests. However, you are allowed to have a trailing comma, which means you can uniformly follow every element with a ,, rather than only separating elements with a ,.

This is really only a typographic convention; while it helps avoid such errors, it's not a substitute for testing.

a = [
    "hello",
    "hi",
    "bye",
]

(Using separate lines for all list elements and the brackets will also make diffs simpler to read, making your code reviewers happy.)

CodePudding user response:

There's no alternate separator that I'm aware of, but there are a few things that might help.

First, you could try initializing the list in one line, if it fits in a reasonable amount of space, where the absence of the separator might be more noticeable.

a = ["hello", "hi", "bye"]

Second, you could try making the entire thing a literal string and then splitting on a newline:

a = """hello
hi
bye""".split('\n')

Third, you could try putting the closing bracket on its own line, and making sure that every line of the list declaration (including the last element) has a comma at the end. This is syntactically valid, and my preferred approach.

a = [
    "hello", 
    "hi", 
    "bye",
]

CodePudding user response:

I think your issue isn't your list or its delimiter. I think your problem is the operation on the elements in the list that is changing the elements. To avoid this length update, I suggest wrapping your operation in a try catch until you figure out what command is altering the elements.

# assuming a is not very big

b = a.copy()

try:
  for i in a:
  *operation on a.list*

except:
  len(a)!= len(b)

del b

There is a step in your code where a updates such that a = a.operation. You can check what your length will be by making a copy b ahead of the try catch. This way you don't have to hard code the length.

  • Related