Loop inside an f-string as a value for an embed

Viewed 259

I have a tuple with some values and I want to send them in an embed. They're inside a dictionary like this dict = {key: [(1, 2, 3), other values here], other key: [(1, 2, 3, 4, 5), other values here]}

Now some of the tuples here are of different lengths and it triggers me if I used a loop to add an embed field since discord doesn't allow the name parameter to be false or null yet. If I use a 0 width whitespace character, there's a big space that I'd rather not have. Tried using ternary operators but it didn't work. I also can't have this for i in range(0, len(dict) - 1): pass since the loop would've already came to an end before I could use it to index the tuple. I also tried doing

value = f'{tuple[i] for i in range(0, len(tuple) - 1)}'

but the bot return <generator object stats.<locals>.<genexpr> at 0x0000012E94AB3200> instead of the values inside the tuple.

Edit:

Thanks to the people who answered! It now works, thanks

2 Answers
tuple[i] for i in range(0, len(tuple) - 1)

Is a generator expression, it doesn't produce any values unless consumed by something like a loop or list()

You can use the equivalent list-comprehension instead:

f'{[tuple[i] for i in range(0, len(tuple) - 1)]}'

Or put the generator inside a list()

f'{list(tuple[i] for i in range(0, len(tuple) - 1))}'

Because your comprehension is not wrapped in [] it is technically a generator expression () (I think this would have worked in python 2.7 though), try this:

my_tuple = (1, 2, 3, 4)

f'{[my_tuple[i] for i in range(0, len(my_tuple) - 1)]}'

Output:

[1, 2, 3]

Also, there are no tuple comprehensions in python because tuples are immutable.

Related