+ 10

How i can make dictionary

withe .key () and value

19th Jul 2017, 12:02 PM
Mj.shabani
Mj.shabani - avatar
7 Answers
+ 41
keys() is usually used to return a list of all the dictionary keys in arbitrary order. Instead, you can use : 1. dict() e.g d=dict(lang='python', extension='.py') #printing d out gives, {'lang': 'python', 'extension': '.py'} 2. dictionary comprehension. e.g squares ={x:x**2 for x in range(3,7)} print(squares) # gives out the key 'x', with the corresponding square of 'x', as value , within the given range : in this case : {3: 9, 4: 16, 5: 25, 6: 36} 3.Now, to retrieve the keys in the above dictionary using keys() do this : squares.keys() #gives out : dict_keys ([3, 4, 5, 6]) #I Hope this is useful to you.
19th Jul 2017, 1:06 PM
<^>washika D<^>
<^>washika D<^> - avatar
+ 6
Litteral assignements: myDict1 = {} # empty dict myDict2 = {'keyname1':42, 'keyname2':'value' } Adding key/value pairs at run time in same way that you update a key myDict1['key'] = 'the value to assign -- not necessarly a string, onviously' You can get a specific key value with: value = myDict2['keyname1'] # 42 .. or with the safer get() method wich avoid key error for undefined keys: value = myDict1.get('undefinedkey',42) (second parameter is the default value to return if key doesn't exist ;))
19th Jul 2017, 1:06 PM
visph
visph - avatar
+ 5
@Reachard: No upvotes for real answers, but 'best answer' mark set to latest unrelated answer? WTF? @@ [edit] ... unrelated answer (what's app number) now deleted and best answer mark set to related one ^^
20th Jul 2017, 3:44 PM
visph
visph - avatar
+ 5
Helping to get justice
21st Jul 2017, 4:53 AM
šŸ‘‘ Prometheus šŸ‡øšŸ‡¬
šŸ‘‘ Prometheus šŸ‡øšŸ‡¬ - avatar
+ 4
@visph I ain't even gonna try to understand... Also posting a phone number on a public website... šŸ¤”
20th Jul 2017, 3:48 PM
mexikodro thanksgiving samples 1/1
mexikodro thanksgiving samples 1/1 - avatar
+ 4
Visph you are pyrely pissed at washika's skills
21st Jul 2017, 4:54 AM
šŸ‘‘ Prometheus šŸ‡øšŸ‡¬
šŸ‘‘ Prometheus šŸ‡øšŸ‡¬ - avatar
+ 4
@S Vengat: @washika deserve the best answer, I have no problem with that... because he fully explain use of dictionnaries, while I only answer to the specific unclear question about 'how make a dictionary' ('with .key () and value' wasnt enough explicit for me :P)
21st Jul 2017, 5:03 AM
visph
visph - avatar