How do you convert a list to a key value pair in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a list, write a Python program to convert the given list to dictionary such that all the odd elements have the key, and even number elements have the value. Since python dictionary is unordered, the output can be in any order.
    Examples: 
     

    Input : ['a', 1, 'b', 2, 'c', 3]
    Output : {'a': 1, 'b': 2, 'c': 3}
    
    Input : ['Delhi', 71, 'Mumbai', 42]
    Output : {'Delhi': 71, 'Mumbai': 42}
    
    
    

      
    Method #1 : dict comprehension
    To convert a list to dictionary, we can use list comprehension and make a key:value pair of consecutive elements. Finally, typecase the list to dict type. 
     

    Python3

    def Convert[lst]:

        res_dct = {lst[i]: lst[i + 1] for i in range[0, len[lst], 2]}

        return res_dct

    lst = ['a', 1, 'b', 2, 'c', 3]

    print[Convert[lst]]

    Output

    {'a': 1, 'b': 2, 'c': 3}
    
    

      
    Method #2 : Using zip[] method
    First create an iterator, and initialize it to variable ‘it’. Then use zip method, to zip keys and values together. Finally typecast it to dict type. 
     

    Python3

    def Convert[a]:

        it = iter[a]

        res_dct = dict[zip[it, it]]

        return res_dct

    lst = ['a', 1, 'b', 2, 'c', 3]

    print[Convert[lst]]

    Output

    {'a': 1, 'b': 2, 'c': 3}
    
    


    View Discussion

    Improve Article

    Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given list of elements, convert each element to a key-value pair dictionary, dividing digits equally.

    Input : test_list = [2323, 82, 129388, 234, 95] 
    Output : {23: 23, 8: 2, 129: 388, 2: 34, 9: 5} 
    Explanation : Digits distributed equally to keys and values.

    Input : test_list = [2323, 82, 129388] 
    Output : {23: 23, 8: 2, 129: 388} 
    Explanation : Digits distributed equally to keys and values. 
     

    Approach: Using list slicing + loop

    In this, we form key-value pair by getting the sliced values from each element by dividing by half the digits for keys and values.

    Python3

    test_list = [2323, 82, 129388, 234, 95]

    print["The original list is : " + str[test_list]]

    res = dict[]

    for ele in test_list:

        mid_idx = len[str[ele]] // 2

        key = int[str[ele][:mid_idx]]

        val = int[str[ele][mid_idx:]]

        res[key] = val

    print["Constructed Dictionary : " + str[res]]

    Output

    The original list is : [2323, 82, 129388, 234, 95]
    Constructed Dictionary : {23: 23, 8: 2, 129: 388, 2: 34, 9: 5}
    

    How do you convert a key

    Summary: To convert a dictionary to a list of tuples, use the dict. items[] method to obtain an iterable of [key, value] pairs and convert it to a list using the list[...] constructor: list[dict. items[]] .

    Can I use a list as a key in Python?

    A dictionary or a list cannot be a key. Values, on the other hand, can literally be anything and they can be used more than once.

    Can Python list have key

    A value in the key-value pair can be a number, a string, a list, a tuple, or even another dictionary. In fact, you can use a value of any valid type in Python as the value in the key-value pair. A key in the key-value pair must be immutable.

    How do I convert a list to a dictionary in Python?

    Since python dictionary is unordered, the output can be in any order. To convert a list to dictionary, we can use list comprehension and make a key:value pair of consecutive elements. Finally, typecase the list to dict type.

    Chủ Đề