473,397 Members | 1,960 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,397 software developers and data experts.

How to create a list of dictionaries?

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 140573
jlm699
314 100+
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 Expert Mod 2GB
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 100+
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
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
by: TJS | last post by:
recommended tools for creating class dictionaries for documentation of vb.net class files ??
0
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...
3
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...
4
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....
4
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: ...
4
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...
5
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
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,...
1
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...

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.