Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
comparing two lists
#17
Let's rewrite it a bit, because I don't think index is what you think it is.
>>> CORRECT_ANSWERS = ["A" , "C" , "A" , "A" , "D" ,
...                        "B" , "C" , "A" , "C" , "B" ,
...                        "A" , "D" , "C" , "A" , "D" ,
...                        "C" , "B" , "B" , "D" , "A"]
>>> for index in CORRECT_ANSWERS:
...   print(index)
...
A
C
A
A
D
B
C
A
C
B
A
D
C
A
D
C
B
B
D
A
If you actually do want the index, and not the elements of the list, then enumerate() is your hero.
>>> for index, item in enumerate(CORRECT_ANSWERS):
...   print(f"{index} => {item}")
...
0 => A
1 => C
2 => A
3 => A
4 => D
5 => B
6 => C
7 => A
8 => C
9 => B
10 => A
11 => D
12 => C
13 => A
14 => D
15 => C
16 => B
17 => B
18 => D
19 => A
If you look back at your previous code, you were looping over range(len(CORRECT_ANSWERS)), but now you're looping over the list directly, which doesn't give indices.
Reply


Messages In This Thread
comparing two lists - by Siylo - Jan-21-2019, 07:20 PM
RE: comparing two lists - by perfringo - Jan-21-2019, 07:29 PM
RE: comparing two lists - by Siylo - Jan-21-2019, 07:31 PM
RE: comparing two lists - by nilamo - Jan-21-2019, 09:23 PM
RE: comparing two lists - by ichabod801 - Jan-21-2019, 08:36 PM
RE: comparing two lists - by Siylo - Jan-21-2019, 08:52 PM
RE: comparing two lists - by ichabod801 - Jan-21-2019, 09:18 PM
RE: comparing two lists - by Siylo - Jan-21-2019, 09:27 PM
RE: comparing two lists - by ichabod801 - Jan-21-2019, 09:51 PM
RE: comparing two lists - by Siylo - Jan-21-2019, 09:58 PM
RE: comparing two lists - by ichabod801 - Jan-21-2019, 10:19 PM
RE: comparing two lists - by Siylo - Jan-21-2019, 10:59 PM
RE: comparing two lists - by perfringo - Jan-22-2019, 08:12 AM
RE: comparing two lists - by Siylo - Jan-22-2019, 06:56 PM
RE: comparing two lists - by ichabod801 - Jan-22-2019, 07:19 PM
RE: comparing two lists - by Siylo - Jan-22-2019, 09:03 PM
RE: comparing two lists - by nilamo - Jan-22-2019, 09:25 PM
RE: comparing two lists - by Siylo - Jan-22-2019, 10:01 PM
RE: comparing two lists - by Siylo - Jan-22-2019, 11:38 PM
RE: comparing two lists - by ichabod801 - Jan-23-2019, 02:09 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Comparing 2 lists BerryK 4 4,634 Apr-15-2017, 09:46 AM
Last Post: volcano63

Forum Jump:

User Panel Messages

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