Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Python Lists and Dictionaries

Posted on 2009-04-03
Medium Priority
390 Views
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
``````
0
Question by:Student_101
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2

LVL 39

Accepted Solution

Roger Baklund earned 2000 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
``````
0

LVL 41

Expert Comment

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

ID: 24065127
sorry, crx is more complete.
0

LVL 39

Expert Comment

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
``````
0

Author Closing Comment

ID: 31566458
Thank you
0

## Featured Post

Question has a verified solution.

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

Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
###### Suggested Courses
Course of the Month6 days, 13 hours left to enroll