Python Forum
How to let the user add 'None' to an specific array in a dictionary?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to let the user add 'None' to an specific array in a dictionary?
#1
Question 
Ok so, the question may sound confusing at first but I'm gonna do my best to explain what I would like to learn in order to improve my programming skills.

Let's say I have a path in which exists 6 folders with the following file images:

Color:
  • Amarillo.png
  • Blanco.png
  • Rojirosado.png
  • Turquesa.png
  • Verde_oscuro.png
  • Zapote.png

Cuerpo:
  • Cuerpo_cangrejo.png

Fondo:
  • Oceano.png

Ojos:
  • Antenas.png
  • Pico.png
  • Verticales.png

Pinzas:
  • Pinzitas.png
  • Pinzotas.png
  • Pinzota_pinzita.png

Puas:
  • Arena.png
  • Marron.png
  • Purpura.png
  • Verde.png

Now, I want the above information to be stored in a dictionary for further use, so I run the code below in the same path in which the folders previously mentioned exist:

import os
# Main method
the_dictionary_list = {}

for name in os.listdir("."):
    if os.path.isdir(name):
        path = os.path.basename(name)
        print(f'\u001b[45m{path}\033[0m')
        list_of_file_contents = os.listdir(path)
        print(f'\033[46m{list_of_file_contents}')
        the_dictionary_list[path] = list_of_file_contents
        print('\n')
print('\u001b[43mthe_dictionary_list:\033[0m')
print(the_dictionary_list)
So after compiling the program above I get my dictionary:

[Image: HqJOR.png]

But here's the problem: After creating the dictionary, how can I let the user decide in which arrays to add a 'None' string as a new value (i.e. not replacing the current ones), meaning that, for instance, if the user wanted to add 'None' only to Puas Array and Pinzas Array, it would generate the following output?

Quote:the_dictionary_list: {
'Color': ['Amarillo.png', 'Blanco.png','Rojirosado.png', 'Turquesa.png', 'Verde_oscuro.png', 'Zapote.png'],
'Cuerpo': ['Cuerpo_cangrejo.png'],
'Fondo': ['Oceano.png'],
'Ojos': ['Antenas.png', 'Pico.png', 'Verticales.png'],
'Pinzas': ['None', 'Pinzitas.png', 'Pinzotas.png', 'Pinzota_pinzita.png'],
'Puas': ['None', 'Arena.png', 'Marron.png', 'Purpura.png', 'Verde.png']}
Reply
#2
Something like this maybe?
the_dictionary_list = {
	'Color': ['Amarillo.png', 'Blanco.png','Rojirosado.png', 'Turquesa.png', 'Verde_oscuro.png', 'Zapote.png'],
	'Cuerpo': ['Cuerpo_cangrejo.png'],
	'Fondo': ['Oceano.png'],
	'Ojos': ['Antenas.png', 'Pico.png', 'Verticales.png'],
	'Pinzas': ['Pinzitas.png', 'Pinzotas.png', 'Pinzota_pinzita.png'],
	'Puas': ['Arena.png', 'Marron.png', 'Purpura.png', 'Verde.png']}

for key, value in the_dictionary_list.items () :
	answer = input (f'Would like to add "None" to {key}? ')
	if answer.lower () == 'y' :
		the_dictionary_list [key].insert (0, 'None')
	print (key, value, '\n')
noahverner1995 likes this post
Reply
#3
(Dec-26-2021, 02:00 AM)BashBedlam Wrote: Something like this maybe?
the_dictionary_list = {
	'Color': ['Amarillo.png', 'Blanco.png','Rojirosado.png', 'Turquesa.png', 'Verde_oscuro.png', 'Zapote.png'],
	'Cuerpo': ['Cuerpo_cangrejo.png'],
	'Fondo': ['Oceano.png'],
	'Ojos': ['Antenas.png', 'Pico.png', 'Verticales.png'],
	'Pinzas': ['Pinzitas.png', 'Pinzotas.png', 'Pinzota_pinzita.png'],
	'Puas': ['Arena.png', 'Marron.png', 'Purpura.png', 'Verde.png']}

for key, value in the_dictionary_list.items () :
	answer = input (f'Would like to add "None" to {key}? ')
	if answer.lower () == 'y' :
		the_dictionary_list [key].insert (0, 'None')
	print (key, value, '\n')

Thank you for your answer Smile, I also developed another one sometime after I created this thread, here it is:

entrada = input('All right, do you want to add a "None" item to an array in the dictionary? (y/n):')

while True:
    
    if entrada == 'y':
        key_entrada = input('Cool, now tell me at which key do you want me to add a "None" item? Type only a valid key name:')
        while True:            
            if key_entrada in the_dictionary_list:
                the_dictionary_list[key_entrada].insert(0, 'None')
                key_entrada = input('Done, do you want to add another one? (y/n):')
                if key_entrada == 'n':
                    entrada = 'n'
                    break
                if key_entrada == 'y':
                    key_entrada = input('Cool, now tell me at which key do you want me to add a "None" item? Type only a valid key name:')
                else:
                    key_entrada = input("Invalid Input, Type 'y' or 'n' without single quotation marks: ")
            else:
                key_entrada = input('That input does not exist in the dictionary, try again, Type only a valid key name:')               
                                   
    if entrada == 'n':
        print('weno')
        break  
    
    elif entrada != 'y' or entrada != 'n':
        entrada = input("Invalid Input, Type 'y' or 'n' without single quotation marks: ")
Now the only thing that doesn't let me sleep is to figure out how to avoid executing the the_dictionary_list[key_entrada].insert(0, 'None') if the current the_dictionary_list already has a key with 'None' as value inside its respective array Shocked , do you know how could I do that to improve the code above? Huh
Reply
#4
if None in the_dictionary_list[key_entrada]:
    print("None is already in the list")
else:
    the_dictionary_list[key_entrada].insert(0, 'None')
noahverner1995 likes this post
Reply
#5
(Dec-26-2021, 09:42 AM)ibreeden Wrote:
if None in the_dictionary_list[key_entrada]:
    print("None is already in the list")
else:
    the_dictionary_list[key_entrada].insert(0, 'None')

Thank you again! Once more, I will update the final version of this program, now the user can choose whether adding a 'None' value to the arrays of one of the available keys only once, or not adding anything at all :

def existe(key, value, diccionario):
    return key in diccionario and value in diccionario[key]

entrada = input('All right, do you want to add a "None" item to an array in the dictionary? (y/n):')

while True:
    
    if entrada == 'y':
        key_entrada = input('Cool, now tell me at which key do you want me to add a "None" item? Type only a valid key name:')
        while True:            
            if key_entrada in the_dictionary_list:
                if existe(key_entrada, 'None', the_dictionary_list) == False:
                    the_dictionary_list[key_entrada].insert(0, 'None')
                    print('\u001b[43mthe_dictionary_list IS UPDATED:\033[0m')
                    print(the_dictionary_list)
                    print('\n')
                    key_entrada = input('Done, do you want to add another one? (y/n):')
                    if key_entrada == 'n':
                        entrada = 'n'
                        break
                    if key_entrada == 'y':
                        key_entrada = input('Cool, now tell me at which key do you want me to add a "None" item? Type only a valid key name:')
                    else:
                        key_entrada = input("Invalid Input, Type 'y' or 'n' without single quotation marks:")
                        while True:
                            if key_entrada == 'n':
                                entrada = 'n'
                                break
                            if key_entrada == 'y':
                                key_entrada = input('Cool, now tell me at which key do you want me to add a "None" item? Type only a valid key name:')
                                break
                            elif key_entrada != 'n' or key_entrada != 'y':
                                key_entrada = input("Invalid Input, Type 'y' or 'n' without single quotation marks:")
                                
                if existe(key_entrada, 'None', the_dictionary_list) == True:
                    key_entrada = input('That input DOES ALREADY EXIST in the dictionary, try again with another key name:') 
            
            elif entrada == 'n':
                break
            
            else:
                key_entrada = input('That input does not exist in the dictionary, try again, Type only a valid key name:')               
                                   
    if entrada == 'n':
        break  
    
    elif entrada != 'y' or entrada != 'n':
        entrada = input("Invalid Input, Type 'y' or 'n' without single quotation marks:")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing specific values out from a dictionary mcoliver88 6 1,433 Apr-12-2023, 08:10 PM
Last Post: deanhystad
  functional LEDs in an array or list? // RPi user Doczu 5 1,635 Aug-23-2022, 05:37 PM
Last Post: Yoriz
  Creating a numpy array from specific values of a spreadsheet column JulianZ 0 1,136 Apr-19-2022, 07:36 AM
Last Post: JulianZ
  User input & Dictionary tfernandes 5 3,662 Apr-03-2020, 07:12 PM
Last Post: tfernandes
  Delete specific lines contain specific words mannyi 2 4,151 Nov-04-2019, 04:50 PM
Last Post: mannyi
  user inputs in constructing a dictionary Exsul 3 3,715 Apr-10-2019, 12:25 PM
Last Post: ichabod801
  Get specific key from multiple keys in python dictionary pradeepkumarbe 0 2,134 Mar-24-2019, 07:23 PM
Last Post: pradeepkumarbe
  User Input to Choose from Dictionary anelliaf 9 25,828 Mar-27-2018, 02:22 PM
Last Post: anelliaf
  can i nest dictionary in array? hsunteik 2 3,061 Feb-02-2017, 01:30 AM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

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