Python Forum
Using Lists as Dictionary Values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Lists as Dictionary Values
#5
Here is the code. The data comes from a file with lines of 15 pieces of text separated by a space. The first piece of text in each line will become the 'key' and a temporary list is made of other pieces of text and that list is to be the 'value' for the respective 'key'. Each time through, the new key is added, but, after all the data is processed, every key ends up with the same list as the value.
for new_line in data:
    new_line = new_line.strip()
    print(f'\n{new_line} : Line Length = {len(new_line)}')

    # read in the rd info / split on whitespace
    rd_info = list(new_line.split())
    print(rd_info)

    # create dictionary
    temp_list.clear()

    rd_name = rd_info[0]
    rd_age = int(rd_info[3])
    rd_yr = int(rd_info[6])
    rd_yr_left = rd_yr
    rd_wt = int(rd_info[13])
    rd_wt_left = rd_wt
    rd_fg = 0
    rd_stp = 0
    temp_list.append(','.join([str(rd_age), str(rd_yr), str(rd_yr_left), str(rd_wt), str(rd_wt_left), str(rd_fg), str(rd_stp)]))
    print(f'Temp List: {temp_list}')
    rd_dict[rd_name] = temp_list

print(rd_dict)
deanhystad write Apr-20-2024, 03:28 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Messages In This Thread
Using Lists as Dictionary Values - by bfallert - Apr-19-2024, 08:30 PM
RE: Using Lists as Dictionary Values - by menator01 - Apr-19-2024, 08:57 PM
RE: Using Lists as Dictionary Values - by buran - Apr-20-2024, 03:49 AM
RE: Using Lists as Dictionary Values - by bfallert - Apr-20-2024, 02:23 PM
RE: Using Lists as Dictionary Values - by bfallert - Apr-20-2024, 09:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace values in Yaml file with value in dictionary PelleH 0 133 Jun-12-2024, 02:40 PM
Last Post: PelleH
  need to compare 2 values in a nested dictionary jss 2 1,009 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Printing specific values out from a dictionary mcoliver88 6 1,569 Apr-12-2023, 08:10 PM
Last Post: deanhystad
  user input values into list of lists tauros73 3 1,225 Dec-29-2022, 05:54 PM
Last Post: deanhystad
Question How to print each possible permutation in a dictionary that has arrays as values? noahverner1995 2 1,875 Dec-27-2021, 03:43 AM
Last Post: noahverner1995
  Getting values from a dictionary brunolelli 5 3,787 Mar-31-2021, 11:57 PM
Last Post: snippsat
  Python dictionary with values as list to CSV Sritej26 4 3,216 Mar-27-2021, 05:53 PM
Last Post: Sritej26
  Conceptualizing modulus. How to compare & communicate with values in a Dictionary Kaanyrvhok 7 4,180 Mar-15-2021, 05:43 PM
Last Post: Kaanyrvhok
  Adding keys and values to a dictionary giladal 3 2,627 Nov-19-2020, 04:58 PM
Last Post: deanhystad
  In this dictionary all the values end up the same. How? Pedroski55 2 2,019 Oct-29-2020, 12:32 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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