Python Forum
Using Lists as Dictionary Values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Lists as Dictionary Values
#9
If you had provided some data that would have been nicer!

You can make an empty dictionary like this:

mydict = {j:'' for j in range(25)}
j is the key and the value is an empty string.

Loop through the keys and replace the empty string with anything you like:

for key in mydict.keys():
    mydict[key] = 'value' + str(key + 1)
Like I said, if you had provided some data, that would have been better!

import uuid
from random import choice, randint
from string import ascii_lowercase

# make a random string for rd_info[0]
def randomString():
    random_string = ''.join([choice(ascii_lowercase) for i in range(4)])
    return random_string

# make a random integer for rd_info[3], rd_info[6], rd_info[13]
def randomInt():
    random_integer = randint(1989, 2022)
    return random_integer

# make some data because you did not provide any data
# make a list of lists
data = [[randomString(), randomInt(), randomInt(), randomInt(), 0, 0] for i in range(25)]

# make some fairly unique keys:
keys = [str(uuid.uuid4()) for i in range(25)]

# make a dictionary of empty strings
mydict = {key:'' for key in keys}

# now populate mydict with your data
# you did not provide a sample of your data so I can't use your data
i = 0
for key in mydict.keys():
    # put a data list in mydict as value
    mydict[key] = data[i]
    i +=1
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
RE: Using Lists as Dictionary Values - by Pedroski55 - Apr-21-2024, 06:55 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  need to compare 2 values in a nested dictionary jss 2 943 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Printing specific values out from a dictionary mcoliver88 6 1,494 Apr-12-2023, 08:10 PM
Last Post: deanhystad
  user input values into list of lists tauros73 3 1,114 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,795 Dec-27-2021, 03:43 AM
Last Post: noahverner1995
  Getting values from a dictionary brunolelli 5 3,691 Mar-31-2021, 11:57 PM
Last Post: snippsat
  Python dictionary with values as list to CSV Sritej26 4 3,103 Mar-27-2021, 05:53 PM
Last Post: Sritej26
  Conceptualizing modulus. How to compare & communicate with values in a Dictionary Kaanyrvhok 7 4,087 Mar-15-2021, 05:43 PM
Last Post: Kaanyrvhok
  Adding keys and values to a dictionary giladal 3 2,561 Nov-19-2020, 04:58 PM
Last Post: deanhystad
  In this dictionary all the values end up the same. How? Pedroski55 2 1,970 Oct-29-2020, 12:32 AM
Last Post: Pedroski55
  Counting the values ​​in the dictionary Inkanus 7 3,801 Oct-26-2020, 01:28 PM
Last Post: Inkanus

Forum Jump:

User Panel Messages

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