473,414 Members | 1,667 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Adding List Elements into dictionary

Avatar19
Expand|Select|Wrap|Line Numbers
  1. List1 = []
  2.  
  3. while True:
  4.     A = raw_input()
  5.     if A:
  6.         A = A.split(" ")
  7.         List1.append(A)
  8.     else:
  9.         break
  10. print List1
  11.  
if the input for example = "A 000", B 10"
print List1 returns
[[A,000],[B,10]]
if want to know how to add these elments into a dictionary, let D = {}, and for e.g.
A is the 1st key, 000 is the value associated with A, etc for the rest of the elements in the List
Thank you for any help i would really appreciate it!
Apr 10 '10 #1

✓ answered by bvdet

Expand|Select|Wrap|Line Numbers
  1. >>> list1 = []
  2. >>> while True:
  3. ...     a = raw_input()
  4. ...     if a:
  5. ...         list1.append(a.split())
  6. ...     else:
  7. ...         break
  8. ...     
  9. >>> print list1
  10. [['A', '000'], ['B', '10']]
  11. >>> dd = {}
  12. >>> for item in list1:
  13. ...     dd[item[0]] = item[1]
  14. ...     
  15. >>> dd
  16. {'A': '000', 'B': '10'}
  17. >>> 

2 28250
bvdet
2,851 Expert Mod 2GB
Expand|Select|Wrap|Line Numbers
  1. >>> list1 = []
  2. >>> while True:
  3. ...     a = raw_input()
  4. ...     if a:
  5. ...         list1.append(a.split())
  6. ...     else:
  7. ...         break
  8. ...     
  9. >>> print list1
  10. [['A', '000'], ['B', '10']]
  11. >>> dd = {}
  12. >>> for item in list1:
  13. ...     dd[item[0]] = item[1]
  14. ...     
  15. >>> dd
  16. {'A': '000', 'B': '10'}
  17. >>> 
Apr 10 '10 #2
Thank you that answered it!!
Apr 10 '10 #3

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

Similar topics

10
by: Des Small | last post by:
Lately I have found myself using a pattern to make new dictionaries quite often, by which I mean twice: def invert(d): nd = {} ).append(key) for k, v in d] return nd def count(l): d = {}
1
by: barnesc | last post by:
Hi again, Since my linear algebra library appears not to serve any practical need (I found cgkit, and that works better for me), I've gotten bored and went back to one of my other projects:...
90
by: Christoph Zwerschke | last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist only since Python 2.4. Anyway, I was thinking about whether it would be possible and desirable to change the old behavior in...
5
by: Stuart | last post by:
Hi everyone, I have this app that needs to insert about 400,000+ items into a sorted list, and I am currently using a simple insert something like this: int index = list.BinarySearch(t); if...
0
by: JB | last post by:
Hi All, I was wondering if there was an easy and straightforward way to bind elements in a ListView control, with the corresponding elements in a List or Dictionary. Ideally I'd like a...
7
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
4
by: =?utf-8?B?TWFjaWVqIEJsaXppxYRza2k=?= | last post by:
Hi Pythonistas! I've got a question about storing tuples in a dictionary. First, a small test case which creates a list of dictionaries: import time list_of_dicts = keys = prev_clk =...
2
by: =?Utf-8?B?TWFobW91ZCBTaGFiYW4=?= | last post by:
i have a problem in adding new listbox items i don't need to allow adding multible items with the same textvalue ex: if current items are: green red
2
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the...
0
by: Gabriel Genellina | last post by:
En Mon, 05 May 2008 08:26:45 -0300, Vaibhav.bhawsar <bhawsar.vaibhav@gmail.comescribió: Use a Queue object to communicate between threads. The language itself doesn't guarantee anything about...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...
0
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...

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.