Python Forum
Syntactic sugar for explicit lists?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Syntactic sugar for explicit lists?
#1
When creating an explicit list, list = [ 'a', 'b', 'c', ... 'z' ], is there a way to "self quote" like @list = qw( a b c ... z) in Perl?
Reply
#2
since strings always has to be inside of quotes i don't think that this is possible in python. Though, what comes close to that what you want to do is when you have a string some_string = "a b c d e f g h i j k l m n o p q r s t u v w x y z" and you then use the split operator
some_string = "a b c ... z"
list = some_string.strip()
then list == ["a", "b", ..., "z"]
But maybe someone else nows a different way :)
Reply
#3
Thanks! I think I should probably stop trying to make Python act like Perl and embrace the new paradigm :)
Reply
#4
(May-10-2018, 06:09 PM)ThiefOfTime Wrote: since strings always has to be inside of quotes i don't think that this is possible in python. Though, what comes close to that what you want to do is when you have a string some_string = "a b c d e f g h i j k l m n o p q r s t u v w x y z" and you then use the split operator
some_string = "a b c ... z"
list = some_string.strip()
then list == ["a", "b", ..., "z"]
But maybe someone else nows a different way :)

Please, never - even in examples - shadow builtin "list" with a custom variable

(May-10-2018, 05:44 PM)Antipaladin Wrote: When creating an explicit list, list = [ 'a', 'b', 'c', ... 'z' ], is there a way to "self quote" like @list = qw( a b c ... z) in Perl?

There are str and repr functions in python - for a "flat" list of scalars, their output is similar
Output:
In [1]: l = list('abcde') In [2]: l Out[2]: ['a', 'b', 'c', 'd', 'e'] In [3]: str(l) Out[3]: "['a', 'b', 'c', 'd', 'e']" In [4]: repr(l) Out[4]: "['a', 'b', 'c', 'd', 'e']"
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#5
You have it.

In [1]: from string import ascii_lowercase

In [2]: print(ascii_lowercase)
abcdefghijklmnopqrstuvwxyz

In [3]: letters = list(ascii_lowercase)

In [4]: print(letters)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
(May-10-2018, 07:10 PM)volcano63 Wrote:
(May-10-2018, 06:09 PM)ThiefOfTime Wrote: since strings always has to be inside of quotes i don't think that this is possible in python. Though, what comes close to that what you want to do is when you have a string some_string = "a b c d e f g h i j k l m n o p q r s t u v w x y z" and you then use the split operator
some_string = "a b c ... z"
list = some_string.strip()
then list == ["a", "b", ..., "z"]
But maybe someone else nows a different way :)

Please, never - even in examples - shadow builtin "list" with a custom variable
I normally would not do it. I just made it that way to fit it to his question. I also would never name a variable "and", "is", "path", etc.. Though, thanks for your tip.
Reply
#7
(May-11-2018, 06:07 AM)ThiefOfTime Wrote: I normally would not do it. I just made it that way to fit it to his question. I also would never name a variable "and", "is", "path", etc.. Though, thanks for your tip.

When I feel too lazy to invent names - usually in forums or in prototyping - I just write list_, dict_, etc.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,367 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Being explicit about the items inside a tuple argument rudihammad 3 2,444 Dec-04-2019, 08:10 AM
Last Post: perfringo
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,261 Mar-20-2019, 08:01 PM
Last Post: stillsen
  convert non-string with explicit base jacklee26 5 16,255 Nov-06-2018, 06:50 AM
Last Post: jacklee26
  Newb: Simple Explicit Formula Duplicitous 1 3,141 May-05-2017, 07:03 PM
Last Post: buran

Forum Jump:

User Panel Messages

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