Median of a list

Python Practice › List Programs › Program 100

Median of a list

EasyList Programs · 100 of 100

Write a Python program to get the median of all the elements of a list.

Input
[2, 4, 6, 7, 5, 8, 3, 11]
Output
[2, 3, 4, 5, 6, 7, 8, 11] 5.5 5.5

Approach

  1. Sort; for an even count average the two middle values.

Solution

Python
import statistics items = [2, 4, 6, 7, 5, 8, 3, 11]s = sorted(items)mid = len(s) // 2median = s[mid] if len(s) % 2 else (s[mid - 1] + s[mid]) / 2print(s, median, statistics.median(items))
Output
[2, 3, 4, 5, 6, 7, 8, 11] 5.5 5.5

Line by line

Line 5

Eight values, so the median is the average of the 4th and 5th sorted values, 5 and 6.

Learn the concept

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

More list programs

← All 100 List Programs

Round every number and sum

Python Practice › List Programs › Program 99

Round every number and sum

EasyList Programs · 99 of 100

Write a Python program to round every number in a list and print the sum of the rounded numbers multiplied by the list length.

Input
[22.4, 4.0, -16.22, -9.1, 11.0, -12.22, 14.2, -5.2, 17.5]
Output
[22, 4, -16, -9, 11, -12, 14, -5, 18]
243

Approach

  1. Round each value with round(), add them, multiply by the length.

Solution

Python
numbers = [22.4, 4.0, -16.22, -9.1, 11.0, -12.22, 14.2, -5.2, 17.5]rounded = [round(n) for n in numbers]print(rounded)print(sum(rounded) * len(numbers))
Output
[22, 4, -16, -9, 11, -12, 14, -5, 18]
243

Line by line

Line 2

round(17.5) is 18 and round(-12.22) is -12; ties go to the even number.

Note

The sum of the rounded numbers is 27; multiplied by the 9 items it is 243.

Learn the concept

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

More list programs

← All 100 List Programs

Decode a run-length encoded list

Python Practice › List Programs › Program 98

Decode a run-length encoded list

MediumList Programs · 98 of 100

Write a Python program to decode a run-length encoded list, where [n, x] means x repeated n times.

Input
[[2, 1], 2, 3, [2, 4], 5, 1]
Output
[1, 1, 2, 3, 4, 4, 5, 1]

Approach

  1. Expand [n, x] pairs; copy plain items.

Solution

Python
encoded = [[2, 1], 2, 3, [2, 4], 5, 1]decoded = []for item in encoded:    if isinstance(item, list):        count, value = item        decoded.extend([value] * count)    else:        decoded.append(item)print(decoded)
Output
[1, 1, 2, 3, 4, 4, 5, 1]

Line by line

Lines 5-6

[value] * count repeats the value.

Learn the concept

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

More list programs

← All 100 List Programs

Remove repeated dictionaries

Python Practice › List Programs › Program 97

Remove repeated dictionaries

EasyList Programs · 97 of 100

Write a Python program to remove repeated dictionaries from a list.

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

Approach

  1. Use a set of each dict’s items (as a frozenset) to remember what was seen.

Solution

Python
data = [{"Hello": 5}, {"student": 7}, {"are": 3}, {"learning": 8}, {"Hello": 5}, {"Language": 8}, {"are": 3}]seen = set()result = []for d in data:    key = frozenset(d.items())    if key not in seen:        seen.add(key)        result.append(d)print(result)
Output
[{'Hello': 5}, {'student': 7}, {'are': 3}, {'learning': 8}, {'Language': 8}]

Line by line

Line 4

frozenset(d.items()) is a hashable fingerprint of the dict, so the check is fast even for long lists.

Learn the concept

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

More list programs

← All 100 List Programs

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