Nikolay Blokhin Facebook Nikolay Blokhin Instagram Nikolay Blokhin Linkedin Nikolay Blokhin Twitter

Hi, I'm Nikolay Blokhin I build the cool things with Python and Swift

A few interesting exercises about python lists

Task 1

You have a list with elements. Write a program that creates a new list with reverse orders of the elements from the original list. Additional condition: your program should n​ot ​use the standard function “list.reverse”.

original_list = [12, 32, 22, 45, 56, 67, 55, 89, 90] result_list = [90, 89, 55, 67, 56, 45, 22, 32, 12]

Answer:

original_list = [12, 32, 22, 45, 56, 67, 55, 89, 90] result_list = [] for item in original_list: result_list.insert(0, item) print('original_list:', original_list) print('result_list: ', result_list)

Task 2

You have a string with text. Create a list that contains all letters from this string.

your_string = 'Python is an interpreted high-level programming language for general-purpose programming.​'

Answer:

your_string = 'Python is an interpreted high-level programming language for general-purpose programming.​' char_list = [] for char in your_string: if char not in char_list: char_list.append(char) print('All chars in the string:') print(char_list) # let's include in the result list only a letters from alphabet # https://docs.python.org/3.7/library/stdtypes.html#str.isalpha letters_list = [] for char in your_string: if char not in letters_list: if char.isalpha(): letters_list.append(char) print('All letters in the string:') print(letters_list)

Task 3

You have a list with integers. Remove from this list all elements with a value less than 10.

your_list = [23, 2, 54, 5, 5, 43, 10, 32, 99, 123423, -28, 0, 9]

Answer:

your_list = [23, 2, 54, 5, 5, 43, 10, 32, 99, 123423, -28, 0, 9] copy_of_original_list = your_list[:] for item in copy_of_original_list: if item < 10: your_list.remove(item) print('Your original list:', copy_of_original_list) print('Your filtered list:', your_list)

Task 4

You have a list of strings. Every of this string is one word. Create from all values of a list the one "human readable" string and print it.

original_list = ['P​ython', 'is', 'an', 'interpreted', 'high-level', 'programming', 'language']

Answer:

original_list = ['Python', 'is', 'an', 'interpreted', 'high-level', 'programming', 'language'] # variant 1 result_string_1 = '' for word in original_list: result_string_1 = result_string_1 + ' ' + word print(result_string_1) # variant 2 result_string_2 = ' '.join(original_list) print(result_string_2)

Task 5

You have a list of strings. Every of this string is one word. But some of the words contain the number "5". It's "wrong" words, other words are "normal". Create from all values of a list with "normal" words the one "human readable" string and print it.

original_list = ['P​ython', 'lol5', 'is', 'ww5wr', 'python5', 'an', 'interpreted', 'high-level', '5ok', 'programming', 'language', '5'​]

Answer:

original_list = ['Python', 'lol5', 'is', 'ww5wr', 'python5', 'an', 'interpreted', 'high-level', '5ok', 'programming', 'language', '5'] # variant 1 result_string_1 = '' for word in original_list: if '5' in word: continue result_string_1 = result_string_1 + ' ' + word print(result_string_1) # variant 2 # it's quite complicated construction for beginners, # so it's just for a demonstration that in Python # every task can be solved in a few ways result_string_2 = ' '.join([word for word in original_list if '5' not in word]) print(result_string_2)