Not logged in
Talk
Contributions
Create account
Log in
Navigation
Main page
About
People
Publications
Teaching
Resources
Research Blog
Wiki Functions
Recent changes
Help
Licensing
Page
Discussion
Edit
View history
Editing
DS4UX (Spring 2016)/Day 3 lecture
(section)
From CommunityData
Jump to:
navigation
,
search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Looping through a dictionary==== The builtin functions <code>.items(), .keys(),</code> and <code>.values()</code> provide you with a lot of flexibility when iterating through dictionaries. >>>for i in your_dict.items(): >>> print(i) ('Bob', 'strawberry') ('Cara', 'mint chip') ('Dora', 'vanilla') ('Alice', 'chocolate') >>>for i_key in your_dict.keys(): >>> print(i_key + " is a key in this dictionary") Bob is a key in this dictionary Cara is a key in this dictionary Dora is a key in this dictionary Alice is a key in this dictionary >>>for i_val in your_dict.values(): >>> print(i_val + " is a value in this dictionary") strawberry is a value in this dictionary mint chip is a value in this dictionary vanilla is a value in this dictionary chocolate is a value in this dictionary >>> for i_key, i_val in your_dict.items(): >>> print(i_key + " is the key for " + i_val) >>> print(i_val + " is the value for " + i_key) >>> print("\n") Bob is the key for strawberry strawberry is the value for Bob ... Cara is the key for mint chip mint chip is the value for Cara ... Dora is the key for vanilla vanilla is the value for Dora ... Alice is the key for chocolate chocolate is the value for Alice
Summary:
Please note that all contributions to CommunityData are considered to be released under the Attribution-Share Alike 3.0 Unported (see
CommunityData:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:
Cancel
Editing help
(opens in new window)
Tools
What links here
Related changes
Special pages
Page information