Apologize for what might seem like an easy one. I'm trying to take two python lists and assign them as values for a dictionary, with the keys defined to match the list characteristics. For example, a list with only upper case words would be values for a key called "upperCase" and a list with lower case words would map as values to another key in the same dictionary called "lowerCase". The lists I've already created by iterating over a string value, splitting them into a main list and then assigning them to new lists (if the index[0] position of the word is uppercase or lowercase. My lists are such:
combDict = {}
isUpCase = ["Hello", "Goodbye", "John", "Jack"]
islowerCase = ["my", "name", "is", "human"]
My key assignments are "Upper" and "Lower"
My goal is to make my dictionary as so:
combDict = {Upper: Hello, Upper: Goodbye, Upper: John, Upper: Jack, Lower: my,
Lower: name, Lower: is, Lower: human}
I've tried a couple of dictionary comprehensions but I cannot seem to get it right. Thoughts? What direction should I go?
CodePudding user response:
In Python, Dictionary keys must be unique, meaning you can't have two keys with the same value. I am interested to know what you want to achieve with that dictionary so that we can propose better solutions to you.
CodePudding user response:
Dictionaries do not take duplicate keys, therefore your goal dictionary is not possible. One solution would be to make combDict as a dictionary with lists as values.
combDict[Upper] = isUpCase
combDict[Lower] = islowerCase
If you are using the dictionary to figure out is a word is upper or lower case, you could set the word as key and its state (upper or lower case) as value, see below
for el in isUpCase:
combDict[el] = Upper
for el in islowerCase:
combDict[el] = Lower