-1

This is what my code is at the moment and I was wondering if I can assign list values like this.(I'm making a quote generator.)

import random as random
quotes = [] 
authors = [] 
quotes[0] = "I have a new philosophy. I'm only going to dread one day at a time." 
authors[0] = "Charles Schulz" 
quotes[1] = "Reality is the leading cause of stress for those in touch with it." 

...

authors[5577] = "Henry David Thoreau" 
quotes[5578] = "Sometimes the cards we are dealt are not always fair. However you must keep smiling and moving on." 
authors[5578] = "Tom Jackson"
x=random.randint(2,5577)
for y in range(0,5579,1):
    print(quotes[y]+" "+author[y])```
chess_lover_6
  • 632
  • 8
  • 22
  • Did you read the error message? It explains why this doesn't work. – kaya3 Feb 04 '20 at 01:02
  • line 5, in quotes[1] = "Reality is the leading cause of stress for those in touch with it." IndexError: list assignment index out of range – chess_lover_6 Feb 04 '20 at 01:04
  • That is what it is showing. – chess_lover_6 Feb 04 '20 at 01:04
  • 1
    *list assignment index out of range* means you are trying to assign to a list, but the index you used is not an index in the list. The error occurs on `quotes[0]`, unless your code is different to what you posted here. The index `0` is out of range because the list is empty, so it does not have an index 0. – kaya3 Feb 04 '20 at 01:06
  • initialize your quotes and authors list ie, `quotes = [None] * 5579` `authors = [None] * 5579` . Then assign to the index. – MjZac Feb 04 '20 at 01:07
  • Yes, that's why it doesn't work. `quotes[0]` assumes there is already a first element in it, and therefore gives an `IndexError`. – icedwater Feb 04 '20 at 01:10
  • _Why doesn’t it work?_ In what way does it not work? Always share the entire error message, and a [mcve]. – AMC Feb 04 '20 at 04:27
  • Does this answer your question? [Why does this iterative list-growing code give IndexError: list assignment index out of range?](https://stackoverflow.com/questions/5653533/why-does-this-iterative-list-growing-code-give-indexerror-list-assignment-index) – AMC Feb 04 '20 at 04:28

4 Answers4

1

As stated in the comments, if you create an empty list, you can't assign values with the [] operator, that's for referencing elements that already exist inside the list (so you can update or read them). For adding new values to an empty list we use the append method like this:

quotes = []
quotes.append("I have a new philosophy. I'm only going to dread one day at a time.")

print(quotes[0])
>>> "I have a new philosophy. I'm only going to dread one day at a time."

You can now modify it because it exists:

quotes[0] =  "Reality is the leading cause of stress for those in touch with it." 
print(quotes[0])
>>>  "Reality is the leading cause of stress for those in touch with it." 

If you try to access index 1 it will give you an IndexError because it only has index 0 (in my example).

marcos
  • 4,473
  • 1
  • 10
  • 24
1

From more on lists, the append() function suggests a roundabout alternative to what you're trying to do:

>>> quotes = []
>>> quotes[0:] = ["first quote"]
>>> quotes[1:] = ["second quote"]
>>> quotes
['first quote', 'second quote']

This requires that both left hand and right hand sides be lists, and makes sure that you can't access lists that haven't had anything assigned to them yet.

As I mentioned in the comment above, IndexError comes from the list not having that element, which prevents one from mistakenly doing something like quotes[55] and expecting it to work too early on.

icedwater
  • 4,701
  • 3
  • 35
  • 50
  • The `append` documentation says this is equivalent, but does not *suggest* it as an alternative. I believe it is only in the docs as a way of specifying `append`'s behaviour more formally. I hope nobody reads this and thinks it is a sensible way to write code. – kaya3 Feb 04 '20 at 01:23
  • I'm not suggesting it's sensible, but it's what looks the most like what was suggested by OP. – icedwater Feb 04 '20 at 02:18
1

You are getting index out of range error since you are trying to access elements from an empty array. You can either initialize the author and quotes list:

authors = [None] * 5579
quotes = [None] * 5579

or a better way to add elements to list would be using the append method.

authors = []
quotes = []

quotes.append("I have a new philosophy. I'm only going to dread one day at a time.") 
authors.append("Charles Schulz")

...
quotes.append("Sometimes the cards we are dealt are not always fair. However you must keep smiling and moving on.") 
authors.append("Tom Jackson")


for author, quote in zip(authors,quotes):
    print("{} {}".format(author, quote))
MjZac
  • 3,476
  • 1
  • 17
  • 28
1

You are doing quotes[0] and so on, while quotes being empty, does not contain an index 0, 1, etc. You should use append() function instead, to add elements to your list.

Or, if you really want to use quotes[0] and so on, then do

quotes = [None] * 5579

or

quotes = [''] * 5579

at the start of the program.

Swati Srivastava
  • 1,102
  • 1
  • 12
  • 18