473,549 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting dictionary values from list of strings to list of list.

1 New Member
I dispose of a dictionary whose values look like a list of strings (d_pro):
Expand|Select|Wrap|Line Numbers
  1. d_pro={'1': ['1,2,3', '4,5,6'], '2': ['1,2,3', '4,5,6']}
I'd like to turn it into a dictionary whose values look like a list of lists (expected output in d_pro_list) for further processing:
Expand|Select|Wrap|Line Numbers
  1. d_pro_list= {'1': [['1,2,3'], ['4,5,6']], '2': [['1,2,3'], ['4,5,6']]}
I'm trying the given code:
Expand|Select|Wrap|Line Numbers
  1. d_pro_list={}
  2. for k in d_pro.keys():
  3.     for v in d_pro.values():
  4.         for i in v:
  5.             d_pro_list[k]= [i]
But the output is like:
Expand|Select|Wrap|Line Numbers
  1. d_pro_list= {'1': ['1,2,3,4,5,6'], '2': ['1,2,3,4,5,6']}
Can you please help me finding my mistakes? Thanks.
Jul 16 '14 #1
1 1213
bvdet
2,851 Recognized Expert Moderator Specialist
I am unsure how you actually want it, so I will show both:
Expand|Select|Wrap|Line Numbers
  1. >>> 
  2. >>> d_pro={'1': ['1,2,3', '4,5,6'], '2': ['1,2,3', '4,5,6']}
  3. >>> for key in d_pro:
  4. ...     d_pro[key] = [item.split(",") for item in d_pro[key]]
  5. ...     
  6. >>> d_pro
  7. {'1': [['1', '2', '3'], ['4', '5', '6']], '2': [['1', '2', '3'], ['4', '5', '6']]}
  8. >>> d_pro={'1': ['1,2,3', '4,5,6'], '2': ['1,2,3', '4,5,6']}
  9. >>> for key in d_pro:
  10. ...     d_pro[key] = [[item] for item in d_pro[key]]
  11. ...     
  12. >>> d_pro
  13. {'1': [['1,2,3'], ['4,5,6']], '2': [['1,2,3'], ['4,5,6']]}
  14. >>> 
Jul 16 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
52098
by: MackS | last post by:
Dear all, I've got several large sets in my program. After performing several operations on these I wish to present one set to the user sorted according to a certain criterion. Is there any direct way to do so? Or must I list = for item in set1:
0
1242
by: Stephen | last post by:
Hi Everyone I'm going to use my first <asp:CheckBoxList> control to create a list of selections which users can choose from and run a search against. In other words they are going to act as the parameters of query. I want to store the values in the web-config and then bind to the values to the checkbox list. Has anyone done anything...
4
1807
by: Siemel Naran | last post by:
Hi. I have found one advantage of returning values through the argument list. It's that we have to store the return value. But when we return by value, we may forgot to store the return value. Consider, void f(int x, int& i); int i, j; f(1, i);
31
2318
by: metiu uitem | last post by:
Say you have a flat list: How do you efficiently get , , ] I was thinking of something along the lines of: for (key,number) in list: print key, number
2
14719
by: Zlatko Matić | last post by:
Hello. How to reference selected values from a multi-select list box, as a criteria in a query ? Is it possible at all? Regards, Zlatko
5
4578
by: metaperl.etc | last post by:
The following program does not work if you uncomment #lis = + list(args) Evidently Python is opting for the nullary constructor list() as opposed to the other one which takes a sequence. But no newcomer would know this. And the Python docs dont give a good example of dealing with taking a sequence of args and converting it to a list. ...
5
1953
by: j1o1h1n | last post by:
Hello, I was trying to create a flattened list of dictionary values where each value is a list, and I was hoping to do this in some neat functionally style, in some brief, throwaway line so that it would assume the insignificance that it deserves in the grand scheme of my program. I had in mind something like this:
5
2319
by: zefciu | last post by:
Hi! I want to embed a function in my python application, that creates a two-dimensional array of integers and passes it as a list (preferably a list of lists, but that is not necessary, as the python function knows the dimensions of this array). As I read the reference, I see, that I must first initialize a list object and then...
3
1199
by: psbasha | last post by:
Hi, Is it not possible to reference other than Dict,tuples ,strings and list datatypes /variables ,when we call the functions? Say int,float and boolean Thanks PSB
2
3557
by: Loginalyn | last post by:
i am doing a Point of Sales sytem.i am having trouble on how will i add all the integer values entered in a list box by clicking a command button with a corresponding integer value. i want to compute the sum of those integers when i click the compute button. can someone give me an idea or a sample code? Thanks in advance!
0
7451
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7473
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5369
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1944
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
764
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.