• Register
1 vote
40 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,050 points

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,930 points

Related questions

0 votes
1 answer 6 views
6 views
I need to know how to remove duplicate elements from list in python.
asked Oct 5 Daniel Anderson 4k points
0 votes
1 answer 37 views
37 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 LizzyM 6.1k points
1 vote
1 answer 14 views
14 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 samhaz 5k points
1 vote
1 answer 21 views
21 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 LizzyM 6.1k points
1 vote
1 answer 19 views
19 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 LizzyM 6.1k points