Solved

Python Lists and Dictionaries

Posted on 2009-04-03
5
352 Views
Last Modified: 2012-05-06
Hey, I have a result in my code as set of dictionaries inside a List as seen here:
[(1, 100000), (2, 200000), (3, 300000)]
So if i want to make variables(lets say a, b and c) which is equal to
a = 1 has 100000
b=  2 has  200000
c= 3 has  300000
how would i go about doing this. This is what i think it should contain:
for i in range(0, len(result)):

    a+=str(i[0][0]) + ' has' + str(i[0][1])

    b+=str(i[1][0]) + ' has ' + str(i[1][1])

    c+=str(i[2][0]) + ' has ' + str(i[2][1])

        print a

        print b

        print c

Open in new window

0
Comment
Question by:Student_101
  • 2
  • 2
5 Comments
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24064784
That's a list of tuples, not dictionaries.

You don't need a loop in this case:
result = [(1, 100000), (2, 200000), (3, 300000)]

a = str(result[0][0]) + ' has ' + str(result[0][1])

b = str(result[1][0]) + ' has ' + str(result[1][1])

c = str(result[2][0]) + ' has ' + str(result[2][1])

print a

print b

print c
 

# using a loop:

for i,j in result:

  print i,'has',j

Open in new window

0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24065124
or, you could even do:

a, b, c = [(1, 100000), (2, 200000), (3, 300000)]

print a
print b
print c
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24065127
sorry, crx is more complete.
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24065189
You could do the same with shorter (but more complex) code:
result = [(1, 100000), (2, 200000), (3, 300000)]

a,b,c = map(lambda t:'%s has %s'%t,result)

print a

print b

print c

Open in new window

0
 

Author Closing Comment

by:Student_101
ID: 31566458
Thank you
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now