1

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.

3 Answers 3

5

You can use print formatting like

for entry in list1:
    print("{name}, {age}  ({place})".format(**entry))
Sign up to request clarification or add additional context in comments.

1 Comment

Real classy! Good one!
2

How about this:

x = ["name", "age"]
for i in list1:
    for k in x:
        print list1[i][k]

4 Comments

I would have answered the same, though the question is really unclear :)
I agree. By "variable" I assume that x is actually programatically set somewhere... If it's fixed then @Hugh's answer is probably better.
Scott - I store in a file what all the things need to be print. In file i['name']+","+i['age'] -- will be there I read and store in a variable and I want to print that
ah. You want something like this then?: for entry in list1: print open("{name},{age}".format(**entry)).read()
0

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 

1 Comment

Deric, In my requirement the list is dynamically created with collection of dictionaries with different columns. 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. For example here I want to print name and age. The parameters I want to read from config file and print the actual value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.