• Register
1 vote
60 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 22 views
22 views
What is the difference between the list and dictionary in python?
asked Oct 10, 2020 sikandar 2.4k points
1 vote
1 answer 22 views
22 views
Problem: I have a dictionary that consists of: Dict = {     "Krishna" : 79,     "Arnav" : 46,     "Paulo" : 90,     "Christian" : 77,     "Renartovic" : 33,     "Wanda" : 65,     "Arteta" : 80,     "Santiago" : 68,     "Jovic" : 88     } I ... compiling this program:   File "val.py", line 13     if "Wanda" in Dict:     ^ IndentationError: unexpected indent Can anyone help me to solve this error?
asked Jun 7, 2020 samhaz 5k points
1 vote
1 answer 27 views
27 views
Problem: I tried to check my dictionares using if the dictionary is in use or not. To check that I wrote something like: def isEmpty(self, dictionary): for element in dictionary: if element: return True return False def onMessage(self, socket, message): if self.isEmpty(self.users ... : socket.send("ONLINE " + ' ' .join(self.users.keys())) But the program is not working properly what should I do?
asked Apr 3, 2020 LizzyM 6.1k points
1 vote
1 answer 23 views
23 views
Problem: I am beginner in pyhton programing. I have a dictionary of string and int and that is: Dictionary = { "Hola": 65, "tart" : 32, "star" : 34, "site" : 55, "pearl" : 88 } Supose I have to remove a key from this dictionary what should I do next.
asked Mar 28, 2020 LizzyM 6.1k points
3 votes
1 answer 20 views
20 views
Problem: Hi all, I am trying to figure out the dictionary related problems in python. Dictionary is really fun to learn and work with. Let’s say, I have a dictionary dict = {'1': 'one', '3': 'three', '2': 'two', '5': 'five', '4': 'four'} If ... needed to check every value inside the dictionary what can I do for that? So, I want to know how to check if a value is in a dictionary python?
asked Mar 24, 2020 Gavin 15.3k points
0 votes
1 answer 47 views
47 views
I wrote a code to check if a dictonary is empty or not. I wrote this code in python : def isEmpty(self, dictionary): for element in dictionary: if element: return True return False def onMessage(self, socket, message): if self.isEmpty(self.users) == False: socket.send("No ... : socket.send("ONLINE " + ' ' .join(self.users.keys())) Does my isEmpty returns true and is there any problem in my code.
asked Mar 11, 2020 LizzyM 6.1k points
1 vote
1 answer 30 views
30 views
Problem: I am starting writing code in python. I wanted to test if a key exists in a dictionary before updating the value for the key. I wrote the following code: if 'key1' in dict.keys(): print "Present" else: print "Thanks" If there any other way to check this, please let me know.
asked Mar 10, 2020 Gavin 15.3k points
0 votes
1 answer 14 views
14 views
I need to know how to remove duplicate elements from list in python.
asked Oct 5, 2020 Daniel Anderson 4k points
0 votes
1 answer 45 views
45 views
I am new to python programming language. I wrote a where I tried to pass a list of functions : def function(f. *args) for f in args print f I can not pass my list of function using this code It gives me error File "main.py", line 1 def function(f. *args) ^ ... : File "main.py", line 1 def function(*args) ^ SyntaxError: invalid syntax How can I solve this and how can I pass a list of function?
asked Mar 12, 2020 LizzyM 6.1k points
1 vote
1 answer 589 views
589 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