Python: copying a list the right way

90% of the time [:] could be replaced by list(). Of course it won’t work for everything since the two are not strictly equivalent, but it is worth trying. Next time you see a [:] try to replace it with list, your code should be more readable. Do it, the devil is in the details.

I would use b = [element for element in a] instead. Using b = list(a) is not explicit enough for me when a is already a list — it’s quite confusing. My options is more explicit in that we’re creating a list from the elements of a, whatever a is.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s