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
4 140573
One way is to simply use the append() function of a list. -
>>> lst = []
-
>>> lst.append({'fn':'b','ln':'d'})
-
>>> lst.append({'fn':'a', 'ln':'c'})
-
>>> print lst
-
[{'fn':'b', 'ln':'d'}, {'fn':'a', 'ln':'c'}]
-
>>> for elem in lst:
-
print elem['fn'], elem['ln']
-
b d
-
a c
-
bvdet 2,851
Expert Mod 2GB
Also using the list append method similar to jlm699's post, you can do something like this: -
keys = ['FirstName', 'LastName', 'SSID']
-
-
name1 = ['Michael', 'Kirk', '224567']
-
name2 = ['Linda', 'Matthew', '123456']
-
-
dictList = []
-
dictList.append(dict(zip(keys, name1)))
-
dictList.append(dict(zip(keys, name2)))
-
-
print dictList
-
for item in dictList:
-
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
>>>
-
dictList.append(dict(zip(keys, name1)))
-
That is awesome... I never knew about zip() ... that is going to help my dictionary implementations immensely!
Cool! Thank you so much for the replies!
I ll try it out, shall post again if I have any further doubt.
Shige
Sign in to post your reply or Sign up for a free account.
Similar topics
by: TJS |
last post by:
recommended tools for creating class dictionaries for documentation of
vb.net class files ??
|
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...
|
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...
|
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....
|
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:
...
|
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...
|
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,
|
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,...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |