I've done a bit of searching but didn't come up with anything.

Is there a way to use multiple placeholders and then use a list with as it assignment. i.e.

l = [1,2,3]
l2 =['blah',50]
print('%i isnt %i isnt %i which isnt %i' %(l[:],l2[1]))

So if the first list has 3 values it would assign the first 3 place holders and then the 2nd list assigns the last place holder.

Sure I can just write

%(l[0],l[1],l[3],l2[1])

But I figured there has to be an easier way, since my list is more than just 3 #'s.

Thanks for any help.

Recommended Answers

All 2 Replies

You would generally use a for loop to iterate through a list. I'm creating one output string, but you could also print within the for loop. Also, please do not use "i", "l" or "o" as single letter variable names as they look too much like numbers.

test_list1 = [1,2,3]
test_list2 =['blah',50]
output_str = "%d" %(test_list1[0])
for n in range(1, len(test_list1)):
    output_str += " isn't %d" % (test_list1[n])
output_str += " which isn't %d" % (test_list2[1])
print(output_str)

Thank you for the input...I don't use those characters in my actual coding I was just trying to hurry and get the post up and give an example in very basic form and just happened to do that. The example you gave is very useful and will use in the future, but is there no way to use slices or something a little more similar to what I posted?

Unfortunately this is an assignment and even though I know and would like to use loops. Even the smallest isnt allowed.

This is my actual coding:

students = grades[0][:6]
print('%15s\t %6.2f\t%6.2f\t%6.2f\t%6.2f\t%6.2f\t%6.2f' %(students[0],students[1],students[2],students[3],students[4],students[5],student_avg[0]))

Essentially "students" is a list from extracting a list out of "grades" which has 4 students in it..which includes the student name and 5 grades. student_avg is an avg of the 5 grades...I dont know why we had to do them separate but we do.
So for testing purposes make:

students = ['John',100,90,50,80,75]
student_avg = [79,10,10,10]
print('%15s\t %6.2f\t%6.2f\t%6.2f\t%6.2f\t%6.2f\t%6.2f' %(students[0],students[1],students[2],students[3],students[4],students[5],student_avg[0]))

And then again 3 more times for the other students.

I had it in a loop because it is really redundant but the prof said no, since he hasn't covered it yet.
So I have redone it w/ out the loops but was hoping still there was a bit more efficient way similar to my first post and avoiding loops.

Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.