1
def substitute(clues,words): 
    subst_words = [] # <== HERE
    for i in range(len(words)):
            word = words[i]
            for j in range(len(clues)):
                word = word.replace(clues[j][1],clues[j][0])
            subst_words.append(word)
    return subst_words

Why is subst_words assigned to an empty pair of brackets before use? Stupid question I know but I am unsure...
Additionally how would I apply a Tuple in substitution of a list, if at all possible?

  • An aside: Instead of writing `for i in range(len(words)):` just do `for word in words:` and also do `for clue in clues` later on. Your code will look much cleaner. – Steven Rumbalski Oct 29 '14 at 15:55
  • Maybe @Steven Rumbalski, but nonetheless in doing so I break the code entirely with a number of run-time errors as 'i' the counter is defined later in the program... – Pontius Pilate VII Oct 29 '14 at 16:22

6 Answers6

3

empty list is used to initiate the appending that is done in the for loop. Then the list is returned at the end. You can collect all your data from the loop and append it to the empty list and use it in your main function.

2

It just initializes subst_words to an empty list. As lists are written with squared brackets in Python, an empty list is just [].

You could have written

subst_words = list()
damienfrancois
  • 52,978
  • 9
  • 96
  • 110
  • Okay @damienfrancois, how would I implement a tuple instead of a list in this code, if at all possible? – Pontius Pilate VII Oct 29 '14 at 16:25
  • tuples are immutable so you cannot extend them, they have no `append` method. A tuple created empty will always be empty. You could create a new tuple at each iteration by concatenating the tuple from the previous iteration with the result of the curret iteration though. See http://stackoverflow.com/questions/1380860/add-variables-to-tuple – damienfrancois Oct 29 '14 at 20:08
0

This is to indicate that you have an empty "list" as opposed to any variables. It also allows you to invoke specific methods like .append used in your code for subst_words.

ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
0

The variable is being initialized to a list.

Uninitialized variables can lead to resource bugs

If it had not been declared it would not be accessible. If it had been declared but initialized to None, you would not be able to use the append function on it.

Also, it is being declared in the outermost scope of the function. Meaning that curly braces nested further within the function will have access to the variable. Had it been declared within the for loop for example you would not be able to access it from outside of the for loop.

cs_alumnus
  • 1,579
  • 16
  • 24
-3
aList = [] # initialises aList to type( aList ) == list

This way python knows beforehand, that this variable is typed as list, even before getting the first list items stored

Andy
  • 49,085
  • 60
  • 166
  • 233
user3666197
  • 1
  • 6
  • 50
  • 92
  • Kindly do not modify the post, if that does not improve the content, which it did not – user3666197 Oct 29 '14 at 15:42
  • 2
    Your formatting is all messed up. Code should go in a code block, horizontal line rules should not go directly under text. – Gillespie Oct 29 '14 at 15:46
  • 2
    @user3666197, you need to stop rolling back those edits. You are simply wrong, chipChocolate and others are right. – Daniel Roseman Oct 29 '14 at 15:49
  • 1
    Variables in Python are not typed; values are. The initialization is not about telling Python the type; it's about providing a value that can be used *at all*. – chepner Oct 29 '14 at 15:49
-3

Set for double brackets is the same to set for "list()"

https://docs.python.org/2/library/functions.html#list

edit:

So you change the question?

1 - If you declare an empty list it's obivious it will be an empty list

2 - If wanna a list with something inside just put something inside

3 - to use a tuple : mytuple = () or mytuple = tuple(). It's the same thing.

If you consider the answer are silly, read again the question. And it's about data types on python so next type search about it on the docs.

Ramon Moraes
  • 477
  • 7
  • 23