BaRud
BaRud

Reputation: 3218

Create a tuple using loop with possibly None element

please see the code below.

I need to create a tuple, where many elements are empty(None). So, I am creating a list and then converting it to a tuple. I know tuple is immutable, and hence, is there a standard way of creating a tuple? (I am worried). How I can do better?

datadict = []
datadict.append(1)
datadict.append(name)
datadict.append(self.KeyEntry.get_text())
for field in self.fields:
    if self.all_fields[field].get_text():
        datadict.append(self.all_fields[field].get_text())
    else:
        datadict.append(None)
datatup = tuple(datadict)

Upvotes: 0

Views: 508

Answers (1)

Paweł Kordowski
Paweł Kordowski

Reputation: 2768

what about this?

data = tuple(self.all_fields[field].get_text() or None for field in self.fields)

Upvotes: 1

Related Questions