Printing Nested Elements from List

A forum for general discussion of the Python programming language.

Printing Nested Elements from List

Postby zacksolutions » Tue Aug 13, 2013 2:13 am

Hello,

I have this list:

Code: Select all
high_scores =  [(Jane ', 100), ('James', 80), ('Steve', 70), ('Jody', 50), ('Joseph', 40)]


I would like to print this list as follows:

    Jane: 100
    James: 80
    Steve: 70
    Jody: 50
    Joseph: 40

Looking if someone can point me in the right direction.

TIA,
zacksolutions
Last edited by micseydel on Tue Aug 13, 2013 2:44 am, edited 1 time in total.
Reason: Locked post.
zacksolutions
 
Posts: 12
Joined: Mon Aug 12, 2013 1:23 pm

Re: Printing Nested Elements from List

Postby micseydel » Tue Aug 13, 2013 2:44 am

What attempt have you made so far?
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1203
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Printing Nested Elements from List

Postby XndrK » Tue Aug 13, 2013 2:46 am

That looks like it can be better done with a dictionary (or associative array).

I'm a noob though, so don't trust me. :p
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm

Re: Printing Nested Elements from List

Postby micseydel » Tue Aug 13, 2013 2:57 am

XndrK wrote:That looks like it can be better done with a dictionary (or associative array).

I'm a noob though, so don't trust me. :p

I don't see why. In fact, if order matters (which we don't know it doesn't) then a dictionary would definitely be a bad idea.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1203
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Printing Nested Elements from List

Postby Yoriz » Tue Aug 13, 2013 11:58 am

Code: Select all
high_scores = [('Jane', 100), ('James', 80), ('Steve', 70), ('Jody', 50),
               ('Joseph', 40)]

print '\n'.join('{0}: {1}'.format(*item) for item in high_scores)

Output
Code: Select all
Jane: 100
James: 80
Steve: 70
Jody: 50
Joseph: 40
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 774
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Printing Nested Elements from List

Postby zacksolutions » Tue Aug 13, 2013 1:21 pm

Yoriz,

That is a beautiful one-liner.

Thank you!
zacksolutions
zacksolutions
 
Posts: 12
Joined: Mon Aug 12, 2013 1:23 pm

Re: Printing Nested Elements from List

Postby XndrK » Fri Aug 16, 2013 11:33 pm

micseydel wrote:
XndrK wrote:That looks like it can be better done with a dictionary (or associative array).

I'm a noob though, so don't trust me. :p

I don't see why. In fact, if order matters (which we don't know it doesn't) then a dictionary would definitely be a bad idea.


Like I said, don't trust me.
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm


Return to General Discussions

Who is online

Users browsing this forum: Google [Bot] and 1 guest