How to cast a list to a dictionary

2024/11/24 20:55:21

I have a list as a input made from tuples where the origin is the 1st object and the neighbour is the 2nd object of the tuple. for example :

inp : lst = [('a','b'),('b','a'),('c',''),('a','c')]
out : {'a': ('a', ['b', 'c']), 'b': ('b', ['a']), 'c': ('c', [])}

first i tried to cast the list into a dictonary, like this

dictonary = dict(lst)

but i got an error say that

dictionary update sequence element #0 has length 1; 2 is required
Answer

The simplest is probably inside a try / except block:

lst = [('a','b'),('b','a'),('c',''),('a','c')]out = dict()for k, v in lst:try:if v != '':out[k][1].append(v)else:out[k][1].append([])except KeyError:if v != '':out[k] = (k, [v])else:out[k] = (k, [])print out

Which gives:

{'a': ('a', ['b', 'c']), 'b': ('b', ['a']), 'c': ('c', [])}
https://en.xdnf.cn/q/120489.html

Related Q&A

Couldnt get rid of (_tkinter.TclError: bad event type or keysym UP) problem

I am running a Linux mint for the first time . I tried coding a python problem but for two days I am continiously facing problems due to Linux interface please This is my code:-import turtleimport time…

WMI lib to start windows service remotely

How do I start a service using the WMI library? The code below throws the exception: AttributeError: list object has no attribute StopServiceimport wmi c = wmi.WMI (servername,user=username,password=p…

TutorialsPoint - Flask – SQLAlchemy not working

I did all that was stated on tutorial point (just copied and pasted), but when I tried to add a student entry,i.e. ‘Add Student’ it givesBad Request The browser (or proxy) sent a request that this se…

Implement f-string like magic to pimp Djangos format_html()

I would like to pimp format_html() of Django. It already works quite nicely, but my IDE (PyCharm) thinks the variables are not used and paints them in light-gray color:AFAIK f-strings use some magic re…

Selecting a set of numbers from a list which add up to a given value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 9…

List of even numbers at even number indexes using list comprehension [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 9…

How do I check if a list of lists exists in a list of lists?

I got a list of lists b and I want to check if they exist in list a which is also a list of lists. Im currently using the following method which is quite time-consuming. Is there a faster way? b = [[…

How do i print out a number triangle in python? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Real time clock display in Tkinter

I want to create real time clock using Tkinter and time library. I have created a class but somehow I am not able to figure out my problem.My codefrom tkinter import *import timeroot = Tk()class Clock:…

cffi export python code to dll , how to read image object in @ffi.def_extern()

i am trying to convert my python code to dll using cffi so i can access this code in my c# apllication, and i am trying to send image my c# code to python function, below is my code to read the file an…