Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List Comprehension Issue
#1
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?
Reply
#2
(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.
Reply
#3
(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!
Reply
#4
(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)
johnywhy likes this post
Reply
#5
(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 :)
Reply
#6
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'
johnywhy likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 490 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Using list comprehension with 'yield' in function tester_V 5 1,262 Apr-02-2023, 06:31 PM
Last Post: tester_V
  list comprehension 3lnyn0 4 1,426 Jul-12-2022, 09:49 AM
Last Post: DeaD_EyE
  Python List Issue Aggie64 5 1,631 Jun-30-2022, 09:15 PM
Last Post: Aggie64
  List to table issue robdineen 2 1,471 Nov-07-2021, 09:31 PM
Last Post: robdineen
  List comprehension used differently coder_sw99 3 1,739 Oct-03-2021, 04:12 PM
Last Post: coder_sw99
  How to invoke a function with return statement in list comprehension? maiya 4 2,865 Jul-17-2021, 04:30 PM
Last Post: maiya
  Calculator code issue using list kirt6405 4 2,292 Jun-11-2021, 10:13 PM
Last Post: topfox
  List comprehension and Lambda cametan 2 2,253 Jun-08-2021, 08:29 AM
Last Post: cametan
  What is the difference between a generator and a list comprehension? Pedroski55 2 2,230 Jan-02-2021, 04:24 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020