473,471 Members | 4,650 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Python beginner with a question about a list of dictionaries

1 New Member
My assignment is:

Make several dictionaries, where the name of each dictionary is the name of a pet. In each dictionary, include the kind of animal and the owner’s name. Store these dictionaries in a list called pets. Next, loop through your list and as you do print everything you know about each pet.


What I have so far:

Expand|Select|Wrap|Line Numbers
  1. rover = {'type': 'dog', 'owner': 'joe'}
  2. blackie = {'type': 'cat', 'owner': 'gail'}
  3. polly = {'type': 'bird', 'owner': 'paul'}
  4. seth = {'type': 'snake', 'owner': 'stan'}
  5.  
  6. pets = [rover, blackie, polly, seth]
  7.  
  8. for pet in pets:
  9.     print("\nPet Name:", "\nType:", pet['type'].title(), "\nPet Owner:", pet['owner'].title())
  10.  
Output so far:

Pet Name:
Type: Dog
Pet Owner: Joe

Pet Name:
Type: Cat
Pet Owner: Gail

Pet Name:
Type: Bird
Pet Owner: Paul

Pet Name:
Type: Snake
Pet Owner: Stan


My Question:

What do I need to add to my code to have the output include the Pet Name.

Desired Output:

Pet Name: Rover
Type: Dog
Pet Owner: Joe

Pet Name: Blackie
Type: Cat
Pet Owner: Gail

Pet Name: Polly
Type: Bird
Pet Owner: Paul

Pet Name: Seth
Type: Snake
Pet Owner: Stan
Feb 24 '16 #1

✓ answered by dwblas

I would include it in the dictionary rover = {'name':'rover', 'type': 'dog', 'owner': 'joe'}

3 1474
dwblas
626 Recognized Expert Contributor
I would include it in the dictionary rover = {'name':'rover', 'type': 'dog', 'owner': 'joe'}
Feb 25 '16 #2
Luk3r
300 Contributor
You could also simply include the item you're iterating.

Expand|Select|Wrap|Line Numbers
  1. ...("\nPet Name:", pet, "\nType:", pet['type'].title()...
I'm not sure if your assignment is suggesting you use one way or the other, but figured I'd point out a second option.
Feb 25 '16 #3
dwblas
626 Recognized Expert Contributor
You could also simply include the item you're iterating.

Expand|Select|Wrap|Line Numbers
  1. ...("\nPet Name:", pet, "\nType:", pet['type'].title()...
That would print the entire contents of each dictionary as pets is a list of dictionaries.
Feb 26 '16 #4

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

Similar topics

13
by: Santanu Chatterjee | last post by:
Hello everybody, I am very new to python. I have a query about list in python. Suppose I have a list a = ,5,6,7,,11,12] I want to know if there is any simple python facility available that...
5
by: Bart Simpson | last post by:
Hi, I want to control 'Scheduled Tasks' in Windows 2003 by python program. But I couldn't find any Python module about win32 'Scheduled Tasks'. I could find only Perl module about it. (see...
6
by: Xah Lee | last post by:
© # -*- coding: utf-8 -*- © © # in Python, there's a special type of © # data structure called keyed list. it © # is a unordered list of pairs, each © # consists of a key and a value. It is ©...
1
by: Scheol Service | last post by:
---------- Forwarded message ---------- From: Scheol Service <scheols@gmail.com> Date: Nov 29, 2006 10:57 PM Subject: Python Question About Compiling. To: mailto:python-list@python.org Im...
11
by: texas22 | last post by:
I am new to programming and am trying to teach myself python. I have two questions how do you use a for loop to print out the lines from a list with the line numbers then the word and then I also...
0
by: marketing-python-bounces | last post by:
This mailing list has been retired. Please use the new Python Advocacy mailing list instead: http://mail.python.org/mailman/listinfo/advocacy Thanks!
5
by: antar2 | last post by:
Hello, I am a beginner in Python and am not able to use a list element for regular expression, substitutions. list1 = list2 = Suppose that I want to substitute the vowels from list2 that...
2
by: cna | last post by:
Hi all and one, how may i learn python. is there any other website except python.org
1
by: anonymous | last post by:
1 Objective to write little programs to help me learn German. See code after numbered comments. //Thanks in advance for any direction or suggestions. tk 2 Want keyboard answer input, for...
0
by: supa | last post by:
Hi. I've coded some stuff a long time ago, and didn't have reason to take up programming until now. I've learned Python 'on-the-go' and now ran into the idea of avoiding global variables of any kind....
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
1
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...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.