Skip to main content

Command Palette

Search for a command to run...

#22 - Python Collections

By Ifeanyi Omeata

Published
1 min read
#22 - Python Collections
I

Hi, I am a Software Developer of 3-4 years specialising in React, Javascript, Node, NextJS, Express, Python, Django, Fast API, SQL and a few other technology stack, with a good background in Networking and Cloud Infrastructure. I am working to become a DevOps Solutions Engineer and happily married to my long time girlfriend.


Topics:


1. Counter from Collections
2. Defaultdict from Collections


1. Counter from Collections


>>Return to Menu

from collections import Counter

nlist = ["b", "e", "a", "e", "c", "e", "c", "d"]
print(Counter(nlist))
print(type(Counter(nlist)))
print(Counter(nlist)['e'])

image.png

from collections import Counter

nlist = ["b", "e", "a", "e", "c", "e", "c", "d"]
print(Counter(nlist))
print(type(Counter(nlist)))
print(Counter(nlist)['e'])

sentence = "The alphabet a to Z."
print(Counter(sentence))
print(type(Counter(sentence)))
print(Counter(sentence)['e'])

image.png


2. Defaultdict from Collections


>>Return to Menu

from collections import defaultdict

mydefaultdict = defaultdict(lambda: 'Sorry, not a key.',{'a':5, 'b':2})
print(mydefaultdict['c'])

mydict = {'a':5, 'b':2}
print(mydict['c'])

image.png

#End


Hope you enjoyed this! :) Follow me for more contents...


Get in Touch:
www.ifeanyiomeata.com
contact@ifeanyiomeata.com

Youtube: https://www.youtube.com/c/IfeanyiOmeata
Linkedin: https://www.linkedin.com/in/omeatai/
Twitter: https://twitter.com/iomeata
Github: https://github.com/omeatai/
Stackoverflow: https://stackoverflow.com/users/2689166/omeatai
Hashnode: https://hashnode.com/@omeatai
Medium: https://medium.com/@omeatai
© 2022

LEARN PYTHON/DJANGO

Part 15 of 37

Learn Python and Django the easy way!..... PYTHON | DJANGO | FASTAPI

Up next

#21 - Python Modules and Packages

By Ifeanyi Omeata