Word lengths as a dictionary

Python Practice › List Programs › Program 96

Word lengths as a dictionary

EasyList Programs · 96 of 100

Write a Python program to build a dictionary of each word and its length.

Input
["Hello", "student", "are", "learning", "Python", "Its", "Language"]
Output
{'Hello': 5, 'student': 7, 'are': 3, 'learning': 8, 'Python': 6, 'Its': 3, 'Language': 8}

Approach

  1. A dict comprehension.

Solution

Python
words = ["Hello", "student", "are", "learning", "Python", "Its", "Language"]print({w: len(w) for w in words})
Output
{'Hello': 5, 'student': 7, 'are': 3, 'learning': 8, 'Python': 6, 'Its': 3, 'Language': 8}

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Remove the 2nd character of each word

Python Practice › List Programs › Program 95

Remove the 2nd character of each word

EasyList Programs · 95 of 100

Write a Python program to remove the 2nd character of each word.

Input
["Hello", "student", "are", "learning", "Python", "Its", "Python", "Language"]
Output
['Hllo', 'sudent', 'ae', 'larning', 'Pthon', 'Is', 'Pthon', 'Lnguage']

Approach

  1. Join the slices before and after index 1.

Solution

Python
words = ["Hello", "student", "are", "learning", "Python", "Its", "Python", "Language"]print([w[0] + w[2:] for w in words])
Output
['Hllo', 'sudent', 'ae', 'larning', 'Pthon', 'Is', 'Pthon', 'Lnguage']

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Capitalise the 3rd character of each word

Python Practice › List Programs › Program 94

Capitalise the 3rd character of each word

EasyList Programs · 94 of 100

Write a Python program to make the 3rd character of each word uppercase.

Input
["Hello", "student", "are", "learning", "Python", "Its", "Python", "Language"]
Output
['HeLlo', 'stUdent', 'arE', 'leArning', 'PyThon', 'ItS', 'PyThon', 'LaNguage']

Approach

  1. Index 2 is the 3rd character; rebuild the word around it.

Solution

Python
words = ["Hello", "student", "are", "learning", "Python", "Its", "Python", "Language"]print([w[:2] + w[2].upper() + w[3:] if len(w) > 2 else w for w in words])
Output
['HeLlo', 'stUdent', 'arE', 'leArning', 'PyThon', 'ItS', 'PyThon', 'LaNguage']
Note

The original sample changed the 4th character (“HelLo”). The 3rd character is index 2, which gives “HeLlo”.

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Replace a word in a list

Python Practice › List Programs › Program 93

Replace a word in a list

EasyList Programs · 93 of 100

Write a Python program to replace “Java” with “Python” in a list of words.

Input
["Hello", "student", "are", "learning", "Java", "Its", "Java", "Language"]
Output
['Hello', 'student', 'are', 'learning', 'Python', 'Its', 'Python', 'Language']

Approach

  1. Map each word, replacing the match.

Solution

Python
words = ["Hello", "student", "are", "learning", "Java", "Its", "Java", "Language"]print(["Python" if w == "Java" else w for w in words])
Output
['Hello', 'student', 'are', 'learning', 'Python', 'Its', 'Python', 'Language']

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Dictionary from sublists

Python Practice › List Programs › Program 92

Dictionary from sublists

EasyList Programs · 92 of 100

Write a Python program to create a dictionary from [key, value] sublists.

Input
[["a", 5], ["b", 8], ["c", 11], ["d", 14], ["e", 23]]
Output
{'a': 5, 'b': 8, 'c': 11, 'd': 14, 'e': 23}

Approach

  1. dict() accepts pairs directly.

Solution

Python
print(dict([["a", 5], ["b", 8], ["c", 11], ["d", 14], ["e", 23]]))
Output
{'a': 5, 'b': 8, 'c': 11, 'd': 14, 'e': 23}

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Tuples to sublists

Python Practice › List Programs › Program 91

Tuples to sublists

EasyList Programs · 91 of 100

Write a Python program to convert the tuples in a list into lists.

Input
[(3, 5), (6, 8), (8, 11), (12, 14), (17, 23)]
Output
[[3, 5], [6, 8], [8, 11], [12, 14], [17, 23]]

Approach

  1. list(t) for each tuple.

Solution

Python
data = [(3, 5), (6, 8), (8, 11), (12, 14), (17, 23)]print([list(t) for t in data])
Output
[[3, 5], [6, 8], [8, 11], [12, 14], [17, 23]]

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Flatten a nested list

Python Practice › List Programs › Program 90

Flatten a nested list

MediumList Programs · 90 of 100

Write a Python program to flatten a nested list.

Input
[0, 12, [22, 32], 42, 52, [62, 72, 82], [92, 102, 112, 122]]
Output
[0, 12, 22, 32, 42, 52, 62, 72, 82, 92, 102, 112, 122]

Approach

  1. Add plain items directly and the contents of sub-lists one by one (recursively for deeper nesting).

Solution

Python
def flatten(items):    flat = []    for item in items:        if isinstance(item, list):            flat.extend(flatten(item))        else:            flat.append(item)    return flat print(flatten([0, 12, [22, 32], 42, 52, [62, 72, 82], [92, 102, 112, 122]]))
Output
[0, 12, 22, 32, 42, 52, 62, 72, 82, 92, 102, 112, 122]

Line by line

Lines 4-5

A sub-list is flattened by calling the same function on it, so any depth works.

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Palindrome strings in a list

Python Practice › List Programs › Program 89

Palindrome strings in a list

EasyList Programs · 89 of 100

Write a Python program to get the palindrome strings from a list.

Input
["data", "python", "oko", "test", "ete"]
Output
['oko', 'ete']

Approach

  1. Keep words equal to their reverse.

Solution

Python
words = ["data", "python", "oko", "test", "ete"]print([w for w in words if w == w[::-1]])
Output
['oko', 'ete']

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Percentage from marks

Python Practice › List Programs › Program 88

Percentage from marks

EasyList Programs · 88 of 100

Write a Python program to calculate the percentage from a list of marks out of 100 each.

Input
[80, 50, 70, 90, 95]
Output
77%

Approach

  1. Total obtained divided by total possible, times 100.

Solution

Python
marks = [80, 50, 70, 90, 95]percent = sum(marks) / (len(marks) * 100) * 100print(f"{percent:.0f}%")
Output
77%

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Bill per fruit purchased

Python Practice › List Programs › Program 87

Bill per fruit purchased

MediumList Programs · 87 of 100

Write a Python program to calculate the bill for each fruit purchased, from a price list and a quantity list.

Input
prices [["apple", 30], ["mango", 50], ["banana", 20], ["lichi", 50]]; bought [["apple", 2], ["mango", 10]]
Output
Fruit: Apple    Bill: 60
Fruit: Mango    Bill: 500
Total: 560

Approach

  1. Turn the price list into a dict for lookup, then multiply price by quantity.

Solution

Python
prices = dict([["apple", 30], ["mango", 50], ["banana", 20], ["lichi", 50]])bought = [["apple", 2], ["mango", 10]]total = 0for fruit, qty in bought:    bill = prices[fruit] * qty    total += bill    print(f"Fruit: {fruit.capitalize():8} Bill: {bill}")print("Total:", total)
Output
Fruit: Apple    Bill: 60
Fruit: Mango    Bill: 500
Total: 560

Line by line

Line 1

dict() accepts a list of [key, value] pairs.

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs