0

This is a program someone else got:

var1 = ["Orange", "Banana"]
var2 = ["Apple", "Pear"]
var3 = ["Banana", "Pear"]
var4 = ["Grapes", "Orange"]
var5 = ["Orange", "Apple"]

newlst = [var1, var2, var3, var4, var5]

user_fruit = input("Whats ur favorite fruit?: ")
user_fruit = user_fruit.split(
    ", ") if ", " in user_fruit else user_fruit.split(",")

for fruit in user_fruit:
    for flist in newlst:
        if fruit in flist:
            print(flist)

The output would print out only the list section. but I was wondering if it was able to also print out variable. For example

var1 = ['Orange' , 'Pear']
var4 = ["Grapes", "Orange"]
var5 = ["Orange", "Apple"]

instead of

['Orange' , 'Pear']
["Grapes", "Orange"]
["Orange", "Apple"]

also if possible, I don't want the program to change too much

2 Answers 2

1

Whenever you need to print out a variable, the best practice is to simply use a dictionary instead. Dicts are meant for this type of stuff:

# Use a dictionary instead of many lists
newdict = {"var1": ["Orange", "Banana"], "var2": ["Apple", "Pear"], "var3": ["Banana", "Pear"], "var4":["Grapes", "Orange"], "var5":["Orange", "Apple"]}

user_fruit = input("Whats ur favorite fruit?: ")
user_fruit = user_fruit.split(", ") if ", " in user_fruit else user_fruit.split(",")

for fruit in user_fruit:
    # Get each key and value in the dictionary
    for key, value in newdict.items():
        if fruit in value:
            # Print out each key and value with a "=" in between
            print(key, "=", value)

Output:

var1 = ['Orange', 'Banana']
var4 = ['Grapes', 'Orange']
var5 = ['Orange', 'Apple']
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, is there a way to use list instead?
@Khang Pham u can look at my code
0

Here is the code for your problem:-

# all lists
var1 = ["Orange", "Banana"]
var2 = ["Apple", "Pear"]
var3 = ["Banana", "Pear"]
var4 = ["Grapes", "Orange"]
var5 = ["Orange", "Apple"]
# this variable will help to access next element in newlst variable 
position=0
# this variable contains the data about the input occurrence in all lists (var1,var2,var3,var4,var5).
occurrence_of_input_in_all_lists=[]
# this variable contains all lists name (var1,var2,var3,var4,var5) in string form
variable_strings=['var1','var2','var3','var4','var5']
newlst = [var1, var2, var3, var4, var5]
user_fruit = input("Whats ur favorite fruit?: ")
user_fruit = user_fruit.split(
    ", ") if ", " in user_fruit else user_fruit.split(",")
for fruit in user_fruit:
    for flist in newlst:
        occurrence_of_input_in_all_lists.append(flist.count(user_fruit[0]))# appending the occurrence of input received if the input dosen't exists in flist it will append '0' to occurrence_of_input_in_all_lists  or if it exists then '1'  will be append to occurrence_of_input_in_all_lists.  
        if occurrence_of_input_in_all_lists[position]==1: #checking  the occurrence of input received
            print(variable_strings[position]+' = '+str(newlst[position]))#printing the final output
        position+=1 #adding 1 to position to access next element from the newlst variable

        

5 Comments

is there a way to shorten the code or simplify it?
@KhangPham yes i changed the code little bit.
@KhangPham if it helped then accept the answer :D
im so sorry, but could you add comments cuz it's a little bit hard for me to understand. I'm trying to learn python.
@KhangPham okkkkk bro wait sometime

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.