Python Forum
Code not working as expected.
Thread Rating:
  • 3 Vote(s) - 3.33 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code not working as expected.
#1
The code below works for the single line of code (#), but not when broken up into 3 different assignments (##). Where am I going wrong?



# ActivePython 2.7.10.12 on Windows 7
# want to create a list of lists 
# [["one","two","three"],["four","five","six"]]
# out of the list all_entries
# works for the single line of code (#), but
# alternate code of individual indices (##) gives 
# [["four","five","six"],["four","five","six"]]


all_entries = ["one","two","three","four","five","six"]

bookmark_entry = [0] * 3

bookmark_list = []

limit = len(all_entries)

for i in range(0,limit,3):
  
  print "i = :" + str(i)    ### just for debugging purposes

  #bookmark_entry = [all_entries[i],all_entries[i + 1],all_entries[i + 2]]   # as single line

  bookmark_entry[0] = all_entries[i]         ##
  print bookmark_entry[0]

  bookmark_entry[1] = all_entries[i + 1]     ##
  print bookmark_entry[1]

  bookmark_entry[2] = all_entries[i + 2]     ##
  print bookmark_entry[2]

  bookmark_list.append(bookmark_entry)

  print "bookmark_entry" + str(bookmark_entry)

print "bookmark list " + str(bookmark_list)
Reply
#2
In the first, you're creating a new list with three elements. In the second, you're setting the first, then second, then third element of an undefined variable.

Instead of bookmark_entry[0] = something, try bookmark_entry.append(something)... and also define the variable somewhere first.
Reply
#3
I thought

bookmark_entry = [0] * 3
defined and declared bookmark _entry to be a list with 3 elements?
Reply
#4
I didn't see that line. Ok, so what's the error you're getting?
Reply
#5
I think I worked it out. I've certainly declared one array, but then I'm appending the same actual list object twice! Thanks for your help anyway.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  code isnt working. nezercat33 1 648 Mar-14-2025, 03:45 AM
Last Post: deanhystad
  Simple code not working properly tmv 2 502 Feb-28-2025, 09:27 PM
Last Post: deanhystad
  I'm new to Python - can someone help with this code as it is not working? lminc123 1 526 Feb-13-2025, 06:13 PM
Last Post: lminc123
  my code is not working erTurko 1 653 Nov-11-2024, 08:43 AM
Last Post: buran
  "Random" module is not working as expected Genericgamemaker 1 939 Jul-25-2024, 04:46 PM
Last Post: Gribouillis
  Python trivial endgame engine is not working as expected max22 0 1,128 Feb-24-2024, 04:41 PM
Last Post: max22
  Simple conditional not working as expected return2sender 8 2,383 Aug-27-2023, 10:39 PM
Last Post: return2sender
  New to Python - Not sure why this code isn't working - Any help appreciated TheGreatNinx 4 2,319 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  code not working when executed from flask app ThomasDC 1 3,216 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  Custom method to handle exceptions not working as expected gradlon93 3 2,089 Dec-22-2022, 07:12 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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