30-Day Coding Challenge
So far in my challenge, I’ve discovered several hacks online, some of which I’ve shared in my previous reports.
It’s part of the process. Each time I sit to code, I discover new ways of working with a function or a data type.
It makes me wonder:
Can I know every single hack in Python language?
Ah, that would be like planning to master all the modules, which I don’t think my brain can take in a lifetime.
What hacks from Days 16 & 17 of my challenge do I plan on sharing?
Day 16: Converting Str to Lists/Tuples
I’ll be honest — I never thought of a situation or the possibility of converting from string to lists and vice versa. Sure, I knew of the str
function but never thought of using it to convert a list into a string. I’ve only used it for converting integers and floats to strings.
I guess there are some functions you never think of applying in some ways.
Day 17: Ways to Add New Key-value Pairs to a Dictionary
Normally, I’ve updated my dictionaries using the update
method:
dict = {'author':'Kester R.', 'pub': 'DSV Logs!', 'action': 'Follow 😉'}
dict.update({'another key': 'Another value'})
# Output: {'author': 'Kester R.', 'pub': 'DSV Logs!', 'action': 'Follow 😉', 'another key': 'Another value'}
But, there are two more ways to do so. Still using the update
method, you can write it this way:
dict.update(new_key = 'New value')
You can even add new key-value pairs without the update
method:
dict['latest key'] = 'Latest value'
However, the second method seems limited since keys used with keyword arguments in the update
method can't have spaces.
On to two more days of discovery and coding.