Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with list Syntax.
#1
I have been working with lists for a while now and I thought it was easy but right now I cannot get it to cooperate.

I have a dictionary full of word patterns as the key and then the list of words as the entry for each key.
I am trying to create a new list if just the entries from one key but it is loading the whole thing like a sting into location 0 instead of 1 word per location in the list. I cannot figure out why.

First I load in my dictionary and functions etc,crate the key I want etc. Everything seems fine so far.

The name of the Dictionary is allPatterns and the key is '0.1.0.2.3.4.5.6.7'.
When I test it, I seem to get the right result. Shown below.
wordPatterns.allPatterns['0.1.0.2.3.4.5.6.7']
['AMAZINGLY', 'ANAEROBIC', 'ANALOGIES', 'ANALOGUES', 'ANALYZERS', 'ANAPHORIC', 'ANARCHISM', 'ANARCHIST', 'ARACHNIDS', 'BOBWHITES', 'COCKTAILS', 'DEDUCTING', 'DEDUCTION', 'ELECTIONS', 'ELECTRIFY', 'ELECTRONS', 'ELEPHANTS', 'ELEVATION', 'ELEVATORS', 'ERECTIONS', 'EXECUTING', 'EXECUTION', 'EXECUTORS', 'EXEMPLARY', 'EXEMPLIFY', 'EXEMPTING', 'EXEMPTION', 'EXERTIONS', 'GIGABYTES', 'GIGAHERTZ', 'MEMORIALS', 'MOMENTARY', 'MUMBLINGS', 'PAPERINGS', 'POPULATED', 'POPULATES', 'REROUTING', 'SISYPHEAN', 'SUSTAINED', 'TUTORIALS']
This is what I would expect.

The problem is when I try to make a list out of this data. Let's call it listA.

listA = [wordPatterns.allPatterns['0.1.0.2.3.4.5.6.7']]
>>> listA[0]
['AMAZINGLY', 'ANAEROBIC', 'ANALOGIES', 'ANALOGUES', 'ANALYZERS', 'ANAPHORIC', 'ANARCHISM', 'ANARCHIST', 'ARACHNIDS', 'BOBWHITES', 'COCKTAILS', 'DEDUCTING', 'DEDUCTION', 'ELECTIONS', 'ELECTRIFY', 'ELECTRONS', 'ELEPHANTS', 'ELEVATION', 'ELEVATORS', 'ERECTIONS', 'EXECUTING', 'EXECUTION', 'EXECUTORS', 'EXEMPLARY', 'EXEMPLIFY', 'EXEMPTING', 'EXEMPTION', 'EXERTIONS', 'GIGABYTES', 'GIGAHERTZ', 'MEMORIALS', 'MOMENTARY', 'MUMBLINGS', 'PAPERINGS', 'POPULATED', 'POPULATES', 'REROUTING', 'SISYPHEAN', 'SUSTAINED', 'TUTORIALS']
>>> listA[1]
Error:
Traceback (most recent call last): File "<pyshell#9>", line 1, in <module> listA[1] IndexError: list index out of range
What Gives? Please help.
Reply
#2
listA = [wordPatterns.allPatterns['0.1.0.2.3.4.5.6.7']]
Here you put extra square brackets, which means wordPatterns.allPatterns['0.1.0.2.3.4.5.6.7'] will be an element in your list (listA). This is also the only element in listA, that is why you get an index error when you try to access second element.
Since that already is a list, you probably don't want a nested list, so don't use that extra square brackets when defining listA.
But if that is what you want, you will need to access elements with listA[0][0], listA[0][1], listA[0][2] etc...
Reply
#3
Seems to be working now. Thank you so much.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Syntax Hitso 4 2,265 May-26-2020, 10:51 AM
Last Post: hussainmujtaba
  What is the correct syntax for list items that need to contain a quotation mark, etc? KaisoArt 7 3,535 Sep-14-2019, 05:26 AM
Last Post: buran
  syntax error on list.sort() jjordan33 3 3,222 Jul-10-2019, 04:57 PM
Last Post: jefsummers
  list syntax blazersnake 6 3,898 Feb-28-2018, 07:48 PM
Last Post: buran

Forum Jump:

User Panel Messages

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