Python Forum
Appending list Trouble Big Time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Appending list Trouble Big Time
#4
The problem is not the function extract_horse_data, though I don't know why you didn't write it as:
def extract_horse_data():
    horses_name.append(xx[horse_count][18])
    for i in range(19)
        horses_info.append(xx[horse_count][i]) # or use horses_info.extend()
My guess is that horse_count is not what you think it is. You probably started with something like this:
all_data = [[str(i)+letter for letter in 'abcdefg'] for i in range(1, 11)]
data = []

def extract_data():
    for i in [1, 3, 5]:
        data.append(all_data[index][i])

for index in range(len(all_data)):
    if int(all_data[index][1][0]) in [3, 6, 8]:
        extract_data()

print(data)
Output:
['3b', '3d', '3f', '6b', '6d', '6f', '8b', '8d', '8f']
The index used by the for loop and the index used by extract_data() are the same variable. But then you changed the code to something like this:
all_data = [[str(i)+letter for letter in 'abcdefg'] for i in range(1, 11)]

index = 0

def extract_data():
    for i in [1, 3, 5]:
        data.append(all_data[index][i])

def get_data(indices):
    for index in range(len(all_data)):
        if int(all_data[index][1][0]) in indices:
            extract_data()

data = []
get_data([3, 6, 8])
print(data)
Output:
['1b', '1d', '1f', '1b', '1d', '1f', '1b', '1d', '1f']
Global variables are bad. The "index" used in extract_data() is the global variable which is zero (or whatever it was last set). The "index" used in get_data() is a local variable. Looping in get_data() is not changing "index" in "extract_data()".

Instead of using global variables you should use function arguments.
all_data = [[str(i)+letter for letter in 'abcdefg'] for i in range(1, 11)]

def extract_data(index):
    for i in [1, 3, 5]:
        data.append(all_data[index][i])

def get_data(indices):
    for index in range(len(all_data)):
        if int(all_data[index][1][0]) in indices:
            extract_data(index)

data = []
get_data([3, 6, 8])
print(data)
This is only a guess. You did not include any code about how xx is made and what clear_data does. You got a bit too overzealous with your redacting and the indentation in your post makes me wonder how all the pieces are related. Also I am left wondering why extract_data appends 19 values, but the repeat in your printout is 9. Are you showing a real printout generated using the extract_data() provided in the post? Is the data messed up in xx?

You mention you can print out the data in the form and it looks ok. What do you mean by that, and where did you put your print statement? Can you amend your example to include the print statement?
Reply


Messages In This Thread
Appending list Trouble Big Time - by Milfredo - Sep-28-2020, 05:37 AM
RE: Appending list Trouble Big Time - by bowlofred - Sep-28-2020, 05:53 AM
RE: Appending list Trouble Big Time - by Milfredo - Sep-28-2020, 06:30 AM
RE: Appending list Trouble Big Time - by deanhystad - Sep-28-2020, 08:25 PM
RE: Appending list Trouble Big Time - by Milfredo - Sep-28-2020, 11:02 PM
RE: Appending list Trouble Big Time - by deanhystad - Sep-29-2020, 02:44 AM
RE: Appending list Trouble Big Time - by Milfredo - Sep-29-2020, 07:35 AM
RE: Appending list Trouble Big Time - by Milfredo - Oct-01-2020, 02:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  trouble reading string/module from excel as a list popular_dog 0 453 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  Time.sleep: stop appending item to the list if time is early quest 0 1,898 Apr-13-2021, 11:44 AM
Last Post: quest
  Reading and appending list MrSwiss 1 1,755 Mar-01-2021, 09:01 AM
Last Post: Serafim
  concatenating 2 items at a time in a python list K11 3 2,397 Oct-21-2020, 09:34 AM
Last Post: buran
  list trouble rediska 3 2,284 Oct-17-2020, 11:17 AM
Last Post: ibreeden
  Appending to list of list in For loop nico_mnbl 2 2,398 Sep-25-2020, 04:09 PM
Last Post: nico_mnbl
  trouble with list array Milfredo 2 2,086 Sep-16-2020, 12:07 AM
Last Post: Milfredo
  Trouble with converting list , dict to int values! faryad13 7 3,818 Sep-04-2020, 06:25 AM
Last Post: faryad13
  appending list of list glennford49 2 2,183 Mar-29-2020, 09:33 AM
Last Post: ibreeden
  Trouble with list function Eggman72 2 1,776 Mar-23-2020, 09:36 PM
Last Post: Eggman72

Forum Jump:

User Panel Messages

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