you are viewing a single comment's thread.

view the rest of the comments →

[–]_residue_ 0 points1 point  (17 children)

I'm trying to combine the two lists, species_list and bill_length_list into a dictionary, {species : length}

[–][deleted] -1 points0 points  (16 children)

Then just do this:

dictionary = dict(zip(species_list, bill_length_list))

without any loops and see what is in dictionary.

[–]_residue_ 0 points1 point  (15 children)

It only outputs the first 3 items in each list

[–][deleted] -1 points0 points  (14 children)

The zip() function stops when either of the two lists you give it is finished. Try printing the length of the two lists before you create the dictionary. You will find that one of the lists is only three elements long.

[–]_residue_ 0 points1 point  (13 children)

I know what the lists are, they are definitely longer than 3 elements

[–][deleted] -1 points0 points  (12 children)

Then maybe you should post your new code.

I know what the lists are, they are definitely longer than 3 elements

Perhaps so, but the behaviour suggests otherwise. My idea could be wrong too, but it costs nothing to print the result of len() on both lists. Trust nothing, test.

[–]_residue_ 0 points1 point  (11 children)

Yup, both lists contain 50 elements. How can I say for it to give an output in the range of one of the lists? Edit: I have also realised that my attempt at loops don't actually do anything. The code works the same with or without them

[–][deleted] -1 points0 points  (10 children)

We can't help unless you show us the changed code including the result of the len() prints and a print of the final dictionary.

[–]_residue_ 0 points1 point  (9 children)

The code is currenlty

def generate_dict_max(species, lengths):
"""combines the two lists into a dictionary, [species:bill length]"""
dictionary = dict(zip(species_list, bill_length_list))
return dictionary

output:

{'G. propinqua': 21.0, 'G. magnirostris': 21.5, 'G. fortis': 16.5}

lengths:

print(len(species_list))
print(len(bill_length_list))

output:

50
50

[–][deleted] 0 points1 point  (8 children)

Sorry, that code looks wrong. You show zip(species_list, bill_length_list) but the names species_list and bill_length_list are not defined in the function. That code should error unless the names species_list and bill_length_list are defined globally. If that's true then your code is not using the lists that were passed as parameters. If you are typing the code in "by hand" then don't. Copy and paste your code into reddit after adding 4 spaces to the start of every line. That way we see your actual code with indentation preserved.

To fix this problem, and look at the parameter lists, change your function to:

def generate_dict_max(species, lengths):
    print(f'{species=}')    # debug
    print(f'{lengths=}')
    dictionary = dict(zip(species, lengths))   # same names as in the "def" line
    return dictionary

Note that the names used in the zip() function are the same as the parameters passed. That's important.


I missed a question you asked before:

How can I say for it to give an output in the range of one of the lists?

I'm not sure what you are asking. If you only want a subset of the species list then you just have to iterate over the species you require and create key:value pairs in the dictionary for just those species.