In my requirement the list is dynamically creating with collection of dictionaries with different columns for each time. So every time what I need to print will be different based on the some logic.
If I create if ..else statement for each case I can print what ever I want.
Here I have too many cases so I dont want to write that many if ...esle statements just to print.
Instead of that I want to read what I need to print from a config file and print the actual value.
Example: list1=[{'name': 'xyz', 'age' : 22, 'place' : 'huj'}, {'name' : 'hjhd", 'age' : 44, 'place' : 'wer'}]
want to print name and age columns
the following code will do my work.
if id == 1:
for i in list1:
i['name']+","+i['age']
elif id == 2:
for i in list1:
i['account']+","+i['spend']
elif id == 3:
for i in list1:
i['percentage']+","+i['rank']
I just want to write only one if else statement. Since I have more than 100 cases.
Instead of writing these many if else statements is there any other way I can handle this by using ConfigParser or any thing else.
You can use print formatting like
for entry in list1:
print("{name}, {age} ({place})".format(**entry))
How about this:
x = ["name", "age"]
for i in list1:
for k in x:
print list1[i][k]
I am also pretty confused by the question but here is another option
list1=[{'name': 'xyz', 'age' : 22, 'place' : 'huj'}, {'name' : 'hjhd", 'age' : 44, 'place' : 'wer'}]
key1 = "name"
key2 = "age"
key3 = "place"
x = [li[key1] + li[key2] + li[key3] for li in list1]
print x
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.