Initialize many string variables
Asked Answered
Y

6

12

I'm initializing a lot of string variables as follows:

a, b, c, d, e, f, g, h = "", "", "", "", "", "", "", ""

You can see that this doesn't look very nice (morever the variables have longer names). Is there some more compact shortcut?

Youngman answered 27/1, 2012 at 14:25 Comment(9)
what are the variables for? Can you use a single list or dictionary instead? Why do you bind all the names at once?Smitherman
The variables are attributes (parameters) like id, name, description, ... and many others extracted from a web page. In the for cycle some values will be "" and some will be filled and a tuple (a, ... h) will be appended to a list of tuples and variables should be reseted for the next round of for cycle to "" (because the old values shouldn't be mixed)Youngman
@J.F.Sebastian I will use dictionary as Rik suggests. But he deleted it.Youngman
But I'm not sure if it will be better to have a list of dictionaries instead of list of tuples.Youngman
tuple(map(some_dict.get, 'abcdefgh')) to convert to tuple. Or better yet Attr(**some_dict) where Attr is a namedtuple.Smitherman
@J.F.Sebastian I'm sorry, I don't exactly understand how you think it. If you have time you could please adjust this snippet.Youngman
example. You could use namedtuple anywhere you use a tuple but with some niceties.Smitherman
Thanks, and is it OK to have a list of dictionaries?Youngman
In Python you could put into a list anything in any combination e.g., L = [1, None, "abc", object(), {"key": 1.0}, ['nested', 'list], MyCustomClass(), 'etc',] it might not be useful but you can.Smitherman
W
20

Definitely more compact:

a=b=c=d=e=f=g=h=""
Weigel answered 27/1, 2012 at 14:28 Comment(3)
Note this is only makes sense when assigning immutable values.Septuple
@MattJoiner, Why you specify as only when immutable values?Unipod
Because in Python they're all assigned the same reference, and if it's mutable, changing one will change them all.Septuple
A
11

As an alternative answer to the a=b=c=...=value solution try:

a, b, c, d, e, f, g, h = [""]*8

Though if you're doing this, it might make sense to put the variables in a list if they have some relation to each other.

Agrigento answered 27/1, 2012 at 14:29 Comment(0)
T
3

While many of the solutions featured here are cute, I'm going to make the case that the following is the most compact solution you should consider:

a = ""
b = ""
c = ""
d = ""
e = ""
f = ""
g = ""

These seven lines of code will be read and comprehended an order of magnitude faster than any of the other solutions. It is extremely clear that each variable is being initialized to an empty string, and your eyes can quickly comprehend this code and move on to other more important code.

And let's be honest, with modern monitors the above code is hardly a waste of screen real estate.

If you find that you need to initialize more than 7 variables then I would suggest your design needs reconsideration. Consider replacing these variables with a more dynamic use of a dictionary or list.

Tema answered 24/4, 2018 at 19:39 Comment(0)
S
2
from collections import defaultdict

To initialize:

attrs = defaultdict(str)

To get 'a' value:

print attrs['a'] # -> ''

To change it:

attrs['a'] = 'abc'
Smitherman answered 27/1, 2012 at 14:56 Comment(0)
B
1

I know this question is about string variables, but to save some lives, remember not to use the same syntax to initialize empty lists:

list1 = list2 = []
list1.append('some string!') # This will be added to both list1 and list2 

Lists are objects and with the above situation, both list1 and list2 will have the same reference in memory.

Button answered 7/3, 2023 at 7:2 Comment(0)
L
0

try this one.

a, b, c, d, e, f, g, h = ["" for i in range(8)]
Laminated answered 5/10, 2017 at 3:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.