3 Cool Python Tricks You Should Know
Photo by Brett Sayles: https://www.pexels.com/photo/two-men-on-skate-park-2020157/

3 Cool Python Tricks You Should Know

In this article, we are going to look at 3 cool Python tricks that you should know.

1. Merging two lists into a dictionary

Sometimes, while working with lists, you may want to change the data type to a dictionary. That is, you may want to combine the lists into a dictionary. To do this, you may have to use the zip() function and the dict() function. The zip() function takes two iterables and pairs the elements. The first element in list one is paired with the first element in list two, and the second element with another second element, etc. The zip() function returns an iterator of tuples. The dict() function will convert the paired elements into a key-value combination, creating a dictionary.

No alt text provided for this image

If an element in the list cannot be paired with another element, then it will be left out. Let’s say list1 has four elements and list2 has five; the fifth item in list2 will be left out.

2. Finding Permutations of a string

Permutations of a string are different orders in which we can arrange the elements of the string. For example, given a str of "ABC", we can rearrange this into – ['ABC', 'ACB', 'BAC', 'BCA', 'CAB', 'CBA']. In Python, the easiest way to find permutations of a string is to use itertools.?Itertools has a permutation class. Here is how we can do it, using itertools.

No alt text provided for this image

3. Unpacking a list using the star operator

Sometimes you want to unpack a list and assign the elements to different variables. Python has a method that you can use to make your life easier. We can use the Python unpacking operator (*). Below is a list of names. We want to get the boy’s name and assign it to the variable "boy." The rest of the names will be assigned to the variable "girls." So, we assign (unpack from the list) the first item on the list to the boy variable. We then add the star * to the variable "girls." By adding * to the name, we are basically telling Python that once it unpacks the boy's name from the list, all the other remaining names must be assigned to the girl's variable name. See the output below:?

No alt text provided for this image

If the name "John" was at the end of the list, we would put the name with the asterisk at the beginning. See below:

No alt text provided for this image

Conclusion

Thank you for reading. Please share this story and subscribe to this newsletter if you are not yet a subscriber. You can also follow me on?LinkedIn?and?Twitter.

....................................................................................................................................................Thank you to everyone; we have reached the milestone of 50K subscribers! Please go to Amazon and get this Python book for free today (kindle edition). Click on the book below.

No alt text provided for this image
James Gatheru

Software Engineer

1 年

Thank you for the article

回复

Hello Benjamin Sir , I am planning to make my career as a data analyst . I want some guidance . Will you please guide me ? Will you please share your contact information with me so that I may personally contact you . It would be a great help .

Charanjeet Singh

Data analyst with gear-up SQL, Python, and data visualisation tools such as Tableau and Ad-Excel .

1 年

Thank you for this article it helpful

Amie Farley

HIS analyst at 3M

1 年

This is great! I am just starting to learn python and I think I can probably use some of these tips.

Manoj Rahar

Persuing BCA | HTML | CSS | JS | Java | SQL | Excel | Power BI

1 年

Thanks for sharing.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了