473,606 Members | 2,409 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create a list of dictionaries?

8 New Member
Hi,

I am quite new to Python & recently I have been given an assignment to create a list of dictionaries.
Can anyone give me an idea of how to do so?

Let say, my dictionary will have 3 keys: "FirstName" , "LastName" , and "SSID".

So the expected output would be:
dictList = [ {'FirstName': 'Michael', 'LastName': 'Kirk', 'SSID': '224567'},
{'FirstName': 'Linda', 'LastName': 'Matthew', 'SSID': '123456'},
{'FirstName': 'Sandra', 'LastName': 'Parkin', 'SSID': '123456'},
{'FirstName': 'Bob', 'LastName': 'Henry', 'SSID': '666666'},
{'FirstName': 'Silvia', 'LastName': 'Perkin', 'SSID': '676767'}]

And assuming after having the above list, how can I retrieve the info of each dictionary entry?

Thanks in advance

Leny
Mar 11 '08 #1
4 140587
jlm699
314 Contributor
One way is to simply use the append() function of a list.

Expand|Select|Wrap|Line Numbers
  1. >>> lst = []
  2. >>> lst.append({'fn':'b','ln':'d'})
  3. >>> lst.append({'fn':'a', 'ln':'c'})
  4. >>> print lst
  5. [{'fn':'b', 'ln':'d'}, {'fn':'a', 'ln':'c'}]
  6. >>> for elem in lst:
  7.             print elem['fn'], elem['ln']
  8. b d
  9. a c
  10.  
Mar 11 '08 #2
bvdet
2,851 Recognized Expert Moderator Specialist
Also using the list append method similar to jlm699's post, you can do something like this:
Expand|Select|Wrap|Line Numbers
  1. keys = ['FirstName', 'LastName', 'SSID']
  2.  
  3. name1 = ['Michael', 'Kirk', '224567']
  4. name2 = ['Linda', 'Matthew', '123456']
  5.  
  6. dictList = []
  7. dictList.append(dict(zip(keys, name1)))
  8. dictList.append(dict(zip(keys, name2)))
  9.  
  10. print dictList
  11. for item in dictList:
  12.     print ' '.join([item[key] for key in keys])
Printed output:

>>> [{'LastName': 'Kirk', 'SSID': '224567', 'FirstName': 'Michael'}, {'LastName': 'Matthew', 'SSID': '123456', 'FirstName': 'Linda'}]
Michael Kirk 224567
Linda Matthew 123456
>>>
Mar 11 '08 #3
jlm699
314 Contributor
Expand|Select|Wrap|Line Numbers
  1. dictList.append(dict(zip(keys, name1)))
  2.  
That is awesome... I never knew about zip() ... that is going to help my dictionary implementations immensely!
Mar 11 '08 #4
shigehiro
8 New Member
Cool! Thank you so much for the replies!

I ll try it out, shall post again if I have any further doubt.

Shige
Mar 11 '08 #5

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

Similar topics

0
862
by: TJS | last post by:
recommended tools for creating class dictionaries for documentation of vb.net class files ??
0
1519
by: Helge V. Larsen | last post by:
I have with some luck tried to program some VBA that writes a text file with all dependencies and dependants for all tables and queries (should be extended with forms and reports). I am able to find the top level dependencies/dependants only as well as all levels (by looping). For each dependency/dependant I have with no success tried to list whether it is a relation, or used in a query, or a LookUp-table used in a table design...
3
1973
by: G | last post by:
Hello, I have a c# ASPX file, and a Code Behind file. This file has contents POSTED to it, around 15 form fields. Rather than manually catch each Request.Form - is there an easy way to simply create a loop which does the following: 1: Look for ALL posted form fields
4
2422
by: colonial | last post by:
I was wondering if what I'm trying to do in Access XP and 2003 is possible. I've looked at countless templates and samples and havn't seen anything like what I want to do to be able to construct it. I've tried manipulating queries, relationships different table joins but to no avail. I'm only a beginner in VBA and know little SQL, I would be truly gratefull if someone can please shed some light on the below. I've been creating a...
4
3287
by: viniiya | last post by:
Hi All, How to Create list of Server/Domains and its User Names from the Network using C#? I tried with Netuserenum and I got only User Names for server but not Domain users. Code I used: NetUserEnum(ServerName, 0, 2, out bufPtr, -1, out EntriesRead, out TotalEntries, out Resume); if (EntriesRead > 0)
4
35782
by: Charlote | last post by:
Hello, I am a beginner in python, and have a question.. I need to create a list of all dates between two dates.. Lets say beggining date= 2008-1-15 enddate= 2008-2-20 how do I get a list dates = ?
5
3683
by: ericdaniel | last post by:
Hi, I'm new to Python and I need to do the following: from this: s = "978654321" to this : Any help is appreciated Thanks,
4
2243
by: shapper | last post by:
Hello, I want to create a List(Of Tag) where Tag is an object with two properties: ID and Name I am creating this list from a CSV string that contains names of many Tags. So I create the List, giving to each Tag only its name (the ID remains empty, as follows: List<Tagform = paper.Tags.Split(',').Select(p =new Tag { Name =
1
4187
by: rythmik1 | last post by:
I need to create an alphabetized list from multiple rows that contain comma separated tags. Example: Row1 -> Lessons, Discussions, Help Row2 -> Discussions, Chat Row3 -> Surveys, Evaluations, Discussions Result needs to be: Chat, Discussions, Evaluations, Help, Lessons, Surveys
0
7955
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8431
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8306
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5966
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5466
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2448
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
1557
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1300
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.