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
  Simple conditional not working as expected return2sender 8 935 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 910 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  code not working when executed from flask app ThomasDC 1 837 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  Custom method to handle exceptions not working as expected gradlon93 3 945 Dec-22-2022, 07:12 PM
Last Post: deanhystad
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 978 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
Exclamation My code is not working as I expected and I don't know why! Marinho 4 1,031 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  My Code isn't working... End3r 4 1,867 Mar-21-2022, 10:12 AM
Last Post: End3r
  set and sorted, not working how expected! wtr 2 1,255 Jan-07-2022, 04:53 PM
Last Post: bowlofred
  I don't undestand why my code isn't working. RuyCab 2 1,956 Jun-17-2021, 03:06 PM
Last Post: RuyCab
  code is not working , can anybody help? RandomPerson69 4 2,850 Mar-22-2021, 04:24 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