+ 6

Python Dictionary - Two keys and one value

Is it possible to have two keys, and one value? a = { "a"|"b": "c", } Or like: a = { "a", "b": "c", }

24th Oct 2018, 7:09 PM
Dolan
Dolan - avatar
29 Respostas
25th Oct 2018, 2:51 PM
Oma Falk
Oma Falk - avatar
+ 9
Two different keys may have the same value, so you can have: a = {“a” : ”c” , “b” : ”c”} if that helps?
24th Oct 2018, 7:35 PM
Russ
Russ - avatar
+ 7
PyDan This will use the tuple ('a', 'b') as a dictionary key, not the letters a and b individually. Dolan Hêriş If you look at a syntax like a = {'a', 'b': 'c', 'd': 'e', 'f', 'g', 'h': 'i'}, it would be really difficult to tell where a value ends and the next key begins. Also, whatever you use before the colon (:) would probably be treated as one key and not as a set of keys, so you'd end up defining a tuple, list or whatever as a key instead of a set of individual keys. I don't think using multiple keys with the same value is possible other than the way Russ explained.
25th Oct 2018, 5:38 AM
Anna
Anna - avatar
+ 5
CodeIt uses a dict comprehension. Dict/list/set comprehensions are a very effective way to create dicts/lists/sets with an "obvious" pattern. For example, if you want to create a list with all numbers from 0 to 10,000 that are divisible by 3, that's a perfect use case for a list comprehension. Your dictionary contains highly specific data. There are eight keys and only two of them share a set of common values. The other key/value pairs consist of unique combinations. That's not a very obvious pattern and not something you'd usually use a dict comprehension for. Thinking about how to set up the comprehension probably needs more time and effort than just defining the dict manually.
25th Oct 2018, 4:54 PM
Anna
Anna - avatar
+ 3
Dolan Hêriş After doing a bit of digging, I’ve found out it is possible to do it in one line, but it does create the exact same dictionary as previously suggested: dct = dict.fromkeys(['a','b'], 'c') print(dct) #{‘b’: ‘c’, ‘a’: ‘c’} To add further key-value pairs, you can use: dct.update(dct.fromkeys(['d','e'], 'f')) print(dct) #{‘e’: ‘f’, ‘d’: ‘f’, ‘b’: ‘c’, ‘a’: ‘c’} Hope this is what you’re after!
25th Oct 2018, 7:24 AM
Russ
Russ - avatar
+ 3
ok... not as a oneliner but easy to add further values https://code.sololearn.com/cXheE2AMrxEf/#py
25th Oct 2018, 5:56 PM
Oma Falk
Oma Falk - avatar
+ 2
You can write it in one line like I did in my comment! Are you trying to get a[“a”] = “c” and a[“b”] = “c” in just one key-value pair? And, if so, why may I ask?
24th Oct 2018, 7:48 PM
Russ
Russ - avatar
+ 2
d = { ("a", "b"): "c" }
25th Oct 2018, 2:26 AM
danvetio
danvetio - avatar
+ 2
Thank you Russ . I will check them out to see if it works or not 🤨 Thanks again 🙏
25th Oct 2018, 7:29 AM
Dolan
Dolan - avatar
+ 2
CodeIt This is exactly what I wanted. Thank you.
25th Oct 2018, 3:00 PM
Dolan
Dolan - avatar
+ 2
you can do it as complicated as u want. I just changed the prog.
25th Oct 2018, 3:51 PM
Oma Falk
Oma Falk - avatar
+ 2
exactly Anna . I tried to add some pairs but it didn’t work! 😔
25th Oct 2018, 4:57 PM
Dolan
Dolan - avatar
+ 2
Dolan Hêriş please tell us the whole idea of your prog. python has an answer for nearky everything. just look here for a further application: https://code.sololearn.com/cJqUMn3ZjkLt/?ref=app
25th Oct 2018, 5:05 PM
Oma Falk
Oma Falk - avatar
+ 2
CodeIt a dictionary for replacing characters between two languages. Some of the characters have two equals and it is OKAY to have them individually in my dictionary but it pops up a question in my mind that what if we could have two keys with one value!? so here we are ;-)
25th Oct 2018, 5:32 PM
Dolan
Dolan - avatar
29th Oct 2018, 1:07 PM
Oma Falk
Oma Falk - avatar
+ 2
I think not, because a dict is language specific. Rules should be stored in an independent format. maybe a csv or other file. we dont have the op. on SL... so this is more a kind of proof of concept.
29th Oct 2018, 1:25 PM
Oma Falk
Oma Falk - avatar
+ 2
Dolan Hêriş i fully trust in u😊😊
29th Oct 2018, 1:39 PM
Oma Falk
Oma Falk - avatar
+ 2
Rugved if you imagine the whole english alphabet in a python dictionay, I cannot use this code inside the dictionary directly. I don’t want to split the alphabet to two or more variables.
29th Oct 2018, 1:40 PM
Dolan
Dolan - avatar
+ 1
Russ Thanks for replying. actually I wanted sth else. Guess I wanna replace “a” and “b” with “c”. but I’m not gonna write it like this: a = { "a": "c", "b": "c" } I want it in one line! 🤨
24th Oct 2018, 7:40 PM
Dolan
Dolan - avatar
+ 1
sure. We have two latin-based characters which equals to one in a RTL. so instead of writing both individually, I wanna use sth like pipe (|) between two keys. but it will cause an error.
24th Oct 2018, 9:47 PM
Dolan
Dolan - avatar