[Python Tips] Destructuring

in #programming6 years ago

This is an easy tip, but an extremely useful feature of Python.

Destructuring lists

Let's take the following sample list:

my_list = ['a', 'b','c']

I want to put each list element into their own variable, you could write a for loop or iterate through the list to create a variable for each element.

Destructuring to the rescue

a, b, c = my_list

That's it!

Real world example

This is all cool and so on, but want to see it in action?

Latest Block

Let's grab the latest block on the Steem blockchain.
Full Block Displayed Here

Transactions in block

First, we want to get just the transactions in the block.
(I am going to skip some for loops and just focus on an individual transaction)

transactions = block['transactions']

If we look at the first transaction we get something like this:

{
'ref_block_num': 50957,
'ref_block_prefix': 355189199,
'expiration': '2018-04-27T18:09:24',
'operations': [
[
'vote',
{
'voter': 'loradavis',
'author': 'veseloff',
'permlink': 'the-deal-coin-innovative-solution -for-business-lending',
'weight': 10000
}
]
],
'extensions': [

],
'signatures': [
'20198cf4469c494880329cc9966fa070e022ab134e09f03f9d9213809f8a4ac207744c79c8dbbdf4976ac0a330977df97ba534d764c45b9cf87ea37f461f075a15 '
]
}

Operations in transaction

We are concerned with the operations section.

operations = transaction['operations']

This will give us all operations for a particular transaction. This is typically only one operation like this:

[
[
'vote',
{
'voter': 'loradavis',
'author': 'veseloff',
'permlink': 'the-deal-coin-innovative-solution-for-business-lending',
'weight': 10000
}
]
]

Operation Type & Operation from Operation

If you look carefully you will see there are two pieces of the initial list, the operation type, and the operation. Let's use destructuring here to break this up quickly and easily from the first (and likely only) operation.

op_type, op = operations[0]

print(op_type)
>> 'vote`
print(op)
>> {'voter': 'loradavis', 'author': 'veseloff', 'permlink': 'the-deal-coin-innovative-solution-for-business-lending', 'weight': 10000}

Taking it further

You can break down the operation using destructuring as well if you wanted to.

Data in op

{'voter': 'loradavis', 'author': 'veseloff', 'permlink': 'the-deal-coin-innovative-solution-for-business-lending', 'weight': 10000}

Destucturing op

voter, author, permlink, weight = op

Conclusion

Destructuring is a powerful Python tool that when used properly can make your code easier to read and easier to write. It works best for data sets that have only a few elements and wouldn't work well destructuring into 10-20+ variables. When working on with data sets with 2-5 elements, it's a really nice tool to have in your toolbox.

My Python Tips Series

Sort:  

I guess the correct word is unpacking. :)

Unpacking is when using the unpacking operators * and **.

wow very excellent programming information

But that is works better in Data set in a few elements! I would like to know about streamline way of formatting strings?

I do not really understand about this post, but I want to know and keep learning. until I can discuss this with you

very excellent programming information

that's some good tips. I am not expert on python.but will check it. Thanks for the tips.

Nice tips, could you please let me know some use case related to above tip. In what scenario it will be useful.

Thanks!

I showed it in the post a real-world use with Steem blockchain...

This is pretty impressive how you do all these programming codes etc., I could never do that.
Your python tip series are very helpful so thank you so much for sharing it with us @themarkymark

I'd love to follow you if you have specific Python tips that could be helpful -- do you have a newsletter?

No, just post every so often on Python when I'm not dealing with drama.

Destructuring does a very good job of making ur codes look organised and presentable at the same time

I think destructuring is especially useful for packing and unpacking variables into a function to make the code neater. Look forward to see your next tip.

You are a good coder

Loading...