Reputation: 335
I would like to use a for loop to get inputs for many questions I have to receive. I managed to make some code, but it seems there should be a better way. Maybe I can reduce the number of variables I'm using?
## <Desired Result>
## onset : 3mo
## location : earth
## character : red
checks = ['onset','location','character']
l1 = ['onset','location','character']
l2 = ['onset','location','character']
for i in range(len(checks)):
l2[i] = input(checks[i])
for i in range(len(checks)):
print(l1[i]+" : "+l2[i])
Upvotes: 1
Views: 150
Reputation: 19403
A few observations on your code:
Note that you never actually change l1
so basically it is unnecessary and wherever you use l1
replace with checks
.
It is not necessary to define l2
this way as you are overriding all its values anyway, so you could just define l2 = []
and then use append
in your loop:
for i in range(len(checks)):
l2.append(input(checks[i]))
Both your loops have exactly the same range, so you could combine them to 1:
for i in range(len(checks)):
l2[i] = input(checks[i])
print(l1[i]+" : "+l2[i])
Now, using list-comprehension and the join
method of strings, you could actually reduce this code to 3 lines (and get rid of l1
):
checks = ['onset', 'location', 'character']
l2 = [input(x) for x in checks]
print("\n".join(checks[i]+" : "+l2[i] for i in range(len(checks))))
Or more neatly using zip
:
print("\n".join(check + " : " + ans for check, ans in zip(checks, l2)))
Lastly, to reduce even one more line (and get rid of l2
):
checks = ['onset', 'location', 'character']
print("\n".join(check + " : " + input(check) for check in checks))
We could also avoid using join
and use the chance to further reduce to one line (getting rid of checks
) using print
's extra arguments and list-unpacking:
print(*(check + " : " + input(check) for check in ['onset', 'location', 'character']), sep='\n')
Upvotes: 3
Reputation: 2331
For a truly 1-line solution to your for-loops, you could do your list comprehension like this:
l2 = [(n, print(l1[i]+" : "+n))[0] for i, n in enumerate([input(x + ": ") for x in checks])]
Ouput:
onseta
locationb
characterc
onset : a
location : b
character : c
EDIT
As others mentioned, this is not best practice, so use something like this:
checks = ['onset','location','character']
l2 = [input(f"Check {n}:\n > ") for n in checks]
print(*(f"{j}: {l2[i]}\n" for i, j in enumerate(checks)), sep="")
Output:
Check onset:
> ok
Check location:
> ok
Check character:
> ok
onset: ok
location: ok
character: ok
Upvotes: 0
Reputation: 5048
You should not initialize the list of desired length and take input for each element. You can use append
method to that.
The following code will help you:
checks = ['onset','location','character']
arr = []
for i in checks:
arr.append(input(i + ' : '))
If you want to reduce the number of lines, you can try the following:
arr = [ input(i + ' : ') for i in ['onset','location','character']]
Upvotes: 0
Reputation: 1490
What you are trying to achieve for is done using List comprehensions.
In your case you can do that in a single line.
l2 = [input(x) for x in checks]
Upvotes: 0