Posts: 21
Threads: 5
Joined: Jan 2024
This works:
choices = 'a', 'b', 'c'
menu = ""
for idx, choice in enumerate(choices):
menu += "\t".join((str(idx), choice, '\n'))
print(menu)
0 a
1 b
2 c This doesn't:
choices = 'a', 'b', 'c'
menu = ["\t".join((str(idx), choice, '\n')) for idx, choice in enumerate(choices)]
print(menu)
['0\ta\t\n', '1\tb\t\n', '2\tc\t\n'] What am i doing wrong?
Posts: 26
Threads: 1
Joined: Jan 2024
(Jan-13-2024, 09:59 PM)johnywhy Wrote: This works:
choices = 'a', 'b', 'c'
menu = ""
for idx, choice in enumerate(choices):
menu += "\t".join((str(idx), choice, '\n'))
print(menu)
0 a
1 b
2 c This doesn't:
choices = 'a', 'b', 'c'
menu = ["\t".join((str(idx), choice, '\n')) for idx, choice in enumerate(choices)]
print(menu)
['0\ta\t\n', '1\tb\t\n', '2\tc\t\n'] What am i doing wrong?
join constructs a string, so what your first code does is string concatenation. List comprehension makes a list, so in your second code you are adding list elements to a list instead of making a string. You need to iterate over them and print them if that's what you want to do.
Posts: 21
Threads: 5
Joined: Jan 2024
(Jan-13-2024, 10:06 PM)sgrey Wrote: in your second code you are adding list elements to a list instead of making a string. You need to iterate over them and print them if that's what you want to do.
I want a joined string, as in expanded for statement. I thought the list comprehension version was identical to the first in behavior. What adjustment would i make to the list comprehension version to make to operationally identical to the normal for ?
thx!
Posts: 26
Threads: 1
Joined: Jan 2024
(Jan-13-2024, 10:16 PM)johnywhy Wrote: (Jan-13-2024, 10:06 PM)sgrey Wrote: in your second code you are adding list elements to a list instead of making a string. You need to iterate over them and print them if that's what you want to do.
I want a joined string, as in expanded for statement. I thought the list comprehension version was identical to the first in behavior. What adjustment would i make to the list comprehension version to make to operationally identical to the normal for ?
thx! List (or other type of) comprehension is a mechanism for creating lists (or other type of collections). Basically you cannot make a list comprehension behave in way that doesn't make a list. You basically have to call join on your list to make it a string. So you would do something like
items = ["\t".join((str(idx), choice)) for idx, choice in enumerate(choices)]
menu = "\n".join(items)
Posts: 21
Threads: 5
Joined: Jan 2024
(Jan-13-2024, 10:30 PM)sgrey Wrote: menu = "\n".join(items) Or, wrap it in the join :
menu = "\n".join(["\t".join((str(idx), choice)) for idx, choice in enumerate(choices)]) A normal for might be easier to read :)
Posts: 1,094
Threads: 143
Joined: Jul 2017
I think this:
menu2 = ["\t".join((str(idx), choice, '\n')) for idx, choice in enumerate(choices)] Does what it should do: it joins (str(idx), choice, '\n') with a \t between them, producing the list menu2, which is what a list comprehension should do:
Output: ['0\ta\t\n', '1\tb\t\n', '2\tc\t\n']
If you want a string from that list, just join it:
s = ''.join(menu2)
Output: '0\ta\t\n1\tb\t\n2\tc\t\n'
|