How to create a dictionary with value as a list in Python

4 Answers

0 votes
dict = {1: ['a', 'b'],
        2: ['c', 'd'],
        3: ['e', 'f'],
        4: ['g', 'h'],
        5: ['i', 'j']}

print(dict)


    
    
'''
run:

{1: ['a', 'b'], 2: ['c', 'd'], 3: ['e', 'f'], 4: ['g', 'h'], 5: ['i', 'j']}

'''

 



answered Apr 11, 2021 by avibootz
0 votes
dict = {1: ['a', 'b'],
        2: ['c', 'd'],
        3: ['e', 'f'],
        4: ['g', 'h'],
        5: ['i', 'j']}

print(dict[2])
print(dict[2][0])
print(dict[2][1])


    
    
'''
run:

['c', 'd']
c
d

'''

 



answered Apr 11, 2021 by avibootz
0 votes
def get_key(val):
    for key, value in dict.items():
         if val == value:
             return key
  
    return "value not exist"
     
dict = {1: ['a', 'b'],
        2: ['c', 'd'],
        3: ['e', 'f'],
        4: ['g', 'h'],
        5: ['i', 'j']}

print(get_key(['e', 'f']))



    
    
'''
run:

3

'''

 



answered Apr 11, 2021 by avibootz
0 votes
def get_key(val):
    for key, value in dict.items():
        if (val in value):
             return key
  
    return "value not exist"
     
dict = {1: ['a', 'b'],
        2: ['c', 'd'],
        3: ['e', 'f'],
        4: ['g', 'h'],
        5: ['i', 'j']}

print(get_key('e'))



    
    
'''
run:

3

'''

 



answered Apr 11, 2021 by avibootz
...