0

i got a Problem, i am creating 2 lists in a function, but i cannot work with them outside the function because it says "NameError: name "ListB" is not defined".

I need the list to create a tuple and write the tuple into a dictonary :)

#
#create 2 lists and 1 dictonary with the same length
#Example: index length is 3
def adder():

    ListB = list()
    ListC = list()

    while True:

        insert1 = input("List2 add: ")
        insert2 = input("List3 add: ")

        ListB.append(insert1)
        ListC.append(insert2)

        print("""if list length is ok, write "ok" """)
        inputPerson = str(input())

        if inputPerson == "ok":
            break

    return ListB, ListC

#run adder
adder = adder()

list2 = [] # create list2/3 with same index length 
list3 = [] # to add ListB to list2 and ListC to list3

list2.append(ListB) #add ListB to list2
list3.append(ListC) #add ListC to list3


tupleList = list(zip(list2, list3)) # take element from list2 and
print(tupleList)  #Test             # list3 in (x, y) order

#create a dictonary with they keyword KeyX X = 0,1,2,3...n : tupleList[0]..[n]
#depending on index length, but X = tupleList[n]!!
dict_List = { \
    'Key0' : tupleList[0],
    'Key1' : tupleList[1],
    'Key2' : tupleList[2],
    }

#print out the result
print("Dict_List:", dict_List)
print("Key0", dict_List['Key0'])
print("Key1", dict_List['Key1'])
print("Key2", dict_List['Key2'])

Right now i dont know how to create a dictonary that will automatically create a new "entry" with KeyX etc.

I hope somebody will help me.

0

1 Answer 1

1

Try something like:

ListB, ListC = adder()

As your function returns two values, you can unpack them like a tuple.

What you have to know is that declaring a variable from within a function make it local and limited to the function's scope. So, you can not access it from outside.

When you call adder(), the returned value does not have any name, it is just a value and you have to assign it to a new variable like you did: adder = adder(). This means that the variable adder now contains the two returned lists.

However, you are overwritting your function (as the names are the same) and this is considered as bad practice. You better should do something like lists = adder().

Then, you can access the created ListB with lists[0]. But as I said, you can also directly unpack it: ListB, ListC = adder().

Sign up to request clarification or add additional context in comments.

3 Comments

I thank you very much, i didnt know what adder = adder() is a bad style. Many programmers are using this in videos.
Maybe you know how to create a dynamic dictonary?
@MartinM If I understand what you are trying to do, you might use dict comprehension: dict_List = {i: tuples[i] for i in range(len(tuples))} with tuples = list(zip(ListB, ListC)). I am not sure you need list2 and list3.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.