Python Dictionary Comprehension

Example 1

add 15 to each value
concerts = { 'Trivium': 100, 'Nine Inch Nails': 120, 'Foo Fighters': 90, 'Queens of the Stone Age': 110 }
new_concert = {key: value + 15 for (key, value) in concerts.items()} print(new_concert)

{‘Trivium’: 115, ‘Nine Inch Nails’: 135, ‘Foo Fighters’: 105, ‘Queens of the Stone Age’: 125}

Example 2 -

Dictionary comprehension with conditional logic

how loud concert is in db compare

filtered_bands = {Band: DB for Band, DB in concerts.items() if DB < 101} print(filtered_bands)

{‘Trivium’: 100, ‘Foo Fighters’: 90}

Example 3

create a dictionary from a list
songs = ['March of the Pigs','Copy of a', 'Wish']
song_length = {Song: len(Song) for Song in songs} print(song_length)

{‘March of the Pigs’: 17, ‘Copy of a’: 9, ‘Wish’: 4}

Example 4

create a dictionary from from two lists
songs = ['March of the Pigs','Copy of a', 'Wish']
albums = ['The Downward Spiral', 'Hesitation Marks', 'Broken']
nin_dict = {songs: albums for songs, albums in zip(songs, albums)} print(nin_dict)

{‘March of the Pigs’: ‘The Downward Spiral’, ‘Copy of a’: ‘Hesitation Marks’, ‘Wish’: ‘Broken’}

Example 5

Band_List_1 = {'While She Sleeps': 305, 'Wage War': 450}
Band_List_2 = {'Architects': 620, 'Breaking Benjamin': 800, 'Slipknot': 1015}
combined_concert_list = {**Band_List_1, **Band_List_2} print(combined_concert_list)

{‘While She Sleeps’: 305, ‘Wage War’: 450, ‘Architects’: 620, ‘Breaking Benjamin’: 800, ‘Slipknot’: 1015}

Example 6

creating a dictionary from a list of tuples

sports = (('Baseball', 'Babe Ruth'), ('Hockey', 'Wayne Gretzky'), ('Cricket', 'Don Bradman'))
sport_dic = {sport[0]: sport[1] for sport in sports} print(sport_dic)

{‘Baseball’: ‘Babe Ruth’, ‘Hockey’: ‘Wayne Gretzky’, ‘Cricket’: ‘Don Bradman’}

Example 7

Functions

def square(x): return x * x
my_dict = {num: square(num) for num in range(1, 11)} print(my_dict)

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}

Example 8

Word Frequency

pig_lyrics = "Nothing can stop me now 'cause I don't care anymore \ Nothing can stop me now 'cause I don't care \ Nothing can stop me now 'cause I don't care anymore \ Nothing can stop me now 'cause I just don't care"
word_freq = {word: pig_lyrics.count(word) for word in set(pig_lyrics.split())} print(word_freq)

{‘Nothing’: 4, ‘now’: 4, ‘anymore’: 2, ‘me’: 4, ‘can’: 4, ‘stop’: 4, ‘I’: 4, “don’t”: 4, ‘just’: 1, “’cause”: 4, ‘care’: 4}

Example 9

Enumerate

lyric_words = ['Nothing', 'can', 'stop', 'me', 'now', 'cause', 'I', 'dont', 'care']
index = {i:j for (i, j) in enumerate(lyric_words)} print(index)

{0: ‘Nothing’, 1: ‘can’, 2: ‘stop’, 3: ‘me’, 4: ‘now’, 5: ’cause’, 6: ‘I’, 7: ‘dont’, 8: ‘care’}

Example 10

Enumerate and Reverse Key:Value in Dictionary

index_v2 = {i:j for (j, i) in enumerate(lyric_words)} print(index_v2)

{‘Nothing’: 0, ‘can’: 1, ‘stop’: 2, ‘me’: 3, ‘now’: 4, ’cause’: 5, ‘I’: 6, ‘dont’: 7, ‘care’: 8}

Example 11

bands_seen_live = {'Beartooth': 5, 'Trivium': 5, 'Shinedown': 4, 'Currents': 4, 'Silent Planet': 4, 'Death Cab For Cutie': 4}
metal_bands = {Band:bands_seen_live[Band] for Band in bands_seen_live.keys() - {'Shinedown', 'Death Cab For Cutie'}} print(metal_bands)

{‘Currents’: 4, ‘Trivium’: 5, ‘Beartooth’: 5, ‘Silent Planet’: 4}

Example 12

rock_bands = {Band:bands_seen_live[Band] for Band in ('Shinedown', 'Death Cab For Cutie')} print(rock_bands)

{‘Shinedown’: 4, ‘Death Cab For Cutie’: 4}

Leave a Reply

Your email address will not be published. Required fields are marked *