• Register
1 vote
77 views

Problem:

I am a beginner in programming language and I choose python for this.So I was doing my programming practice. I was looking for a way to convert list to dictionary python. It should applicable. Thank you

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

1 vote

Answer:

Supose you have a list that contains:

list = ['x', 1, 'y', 2, 'z', 3] 

The first way we can use is dict comprehension:

def Conversion(list): 
    res_dct = {list[i]: list[i + 1] for i in range(0, len(list), 2)} 
    return res_dct 
          

list = ['x', 1, 'y', 2, 'z', 3] 
print(Conversion(list))

And the output will be:

{'x': 1, 'y': 2, 'z': 3}

And the other method we can use is called zip method:

def Conversion(a): 
    it = iter(list) 
    res_dct = dict(zip(it, it)) 
    return res_dct 
          
 
list = ['x', 1, 'y', 2, 'z', 3] 
print(Conversion(list)) 

Happy coding. If you dont understand this fl free to ask me anything.

11 6 4
34,950 points

Related questions

0 votes
1 answer 6 views
6 views
Problem: I am unable to understand this problem .. Please suggest me a better solution to solve it .. creating a dictionary from a list in python
asked Apr 21 Ifra 37.2k points
0 votes
1 answer 6 views
6 views
Problem: Can anyone told me the difference between list ruble and dictionary in python?
asked Apr 18 niyatiry 3.9k points
0 votes
1 answer 8 views
8 views
Problem: How to I Solve this: get list of keys from dictionary python HELP! I need help understanding this: get list of keys from dictionary python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 5 Mashhoodch 13k points
0 votes
1 answer 46 views
46 views
What is the difference between the list and dictionary in python?
asked Oct 10, 2020 sikandar 2.4k points
1 vote
1 answer 796 views
796 views
Problem : I am very new to Django. My current task is to store the form field variable with the help of Django cache and I have already added the Django form in my view. I am often facing below error and as I am very new to Django so unable to understand the below ...         form = Formulary() return render(request, 'form.html', {'form' : form}) I am looking for Django expert to fix above error.
asked May 1, 2020 stewart 4k points
0 votes
1 answer 14 views
14 views
Problem: How to solve this error: How do I list all files of a directory in python ?
asked Mar 19 Wafa Abu Yousef 6.1k points
0 votes
1 answer 10 views
10 views
Problem: How can I list all the files in a directory in python and add them to a list?
asked Mar 17 sasha 16.2k points
0 votes
1 answer 39 views
39 views
Problem: I am new to python. Can anyone help me out? How to delete a list in python? Thanks!!
asked Mar 5 Samiur1 38.4k points
0 votes
2 answers 34 views
34 views
How can I list all files of a directory in Python and add them to a list ?
asked Oct 24, 2020 psandprop 2.4k points
0 votes
1 answer 30 views
30 views
I need to know how to remove duplicate elements from list in python.
asked Oct 5, 2020 Daniel Anderson 4k points