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 140587
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
Recognized Expert Moderator Specialist
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 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...
|
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
|
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...
|
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)
| |
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 = ?
|
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, giving to each Tag only its name (the ID remains
empty, as follows:
List<Tagform = paper.Tags.Split(',').Select(p =new Tag { Name =
|
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
|
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,...
|
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...
| |
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |