analitics

Pages

Wednesday, July 15, 2020

Python 3.8.3 : Lists in Python 3 - part 001.

I am currently working on a project that involves the use of complex data structures and lists and my time is limited.
This led me to start a new series of python tutorials on python lists.
I realized that the lists had no substantial changes in the evolution of the python programming language, see the official documentation.
You will find on the internet a lot of questions related to lists, algorithms, and problems involving lists.
If you are not a beginner then it will seem boring at first but over time I will try to draw attention to really significant elements in python programming with lists.
Let's start taking significant steps in using lists.
Before using the methods of list objects, let's create some lists:

# create a empty list 
my_list_001 = []
# fill the list with 4 consecutive numbers from 0 to 3 
my_list_001 = list(range(0,4))
# show the list 
my_list_001
[0, 1, 2, 3]
# set the variable n with value 4
n = 4
# create a list with 4 zeroes 
list_of_zeros = [0] * n
# show the list
listofzeros
[0, 0, 0, 0]
# import string python package 
import string
# create a string with all ascii letters
my_letters = string.ascii_letters
# use list to create a list with all letters
list(my_letters)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
# create a nested list is a list can also have another list as an item
my_list = ["catafest", [1,9,7,6], ["!"]]
# show the list
my_list
['catafest', [1, 9, 7, 6], ['!']]
# access elements from a list with index operator pozitive and negative [] 
my_list[0]
'catafest'
my_list[-1]
['!']
my_list[-2]
[1, 9, 7, 6]
my_list[-3]
'catafest'
# test conditions boolean
my_list[-3] == my_list[0]
True
my_list[-3] != my_list[0]
False
my_list_001[1] > listofzeros[1]
True
# sum an list element with another
sum_of_list_elemenet = my_list_001[1] + listofzeros[1]
# show result
sum_of_list_elemenet
1
# iterating through a list
for list_element in my_list:
...     print(list_element)
...
catafest
[1, 9, 7, 6]
['!']
# iterating each list  
for list_element in my_list:
...     for e in list_element:
...             print(e)
...
c
a
t
a
f
e
s
t
1
9
7
6
!
# create a list with all letters using chr and ord built-in functions
[chr(i) for i in range(ord('a'),ord('z')+1)]
# the result is
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
 'u', 'v', 'w', 'x', 'y', 'z']
# create a binary list from a integer
output = [int(x) for x in '{:08b}'.format(1976)]
# show the result of the list
output
[1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0]
# show the result just for on element of the list
output[3]
1
# convert a binary list to integer named result and set first with value 0
# using the bitwise left shift operator
result = 0
for digits in output:
    result = (result << 1) | digits
# show result of conversion from binary to integer
result
1976
# create a hexadecimal list from a list
hex_list = [hex(x) for x in my_list_001]
# show the hexadecimal list using the hex built-in function
hex_list
['0x0', '0x1', '0x2', '0x3']
# convert hexadecimal list to int list using the int 
int_list = [int(x,0) for x in hex_list]
# show the result
int_list
[0, 1, 2, 3]
# convert hexadecimal list to float list using the float.fromhex
float_list = [float.fromhex(x) for x in hex_list]
# show the result
float_list
[0.0, 1.0, 2.0, 3.0]
# create list using math python module 
import math
[2*x for x in my_list_001]
[0, 2, 4, 6]
[math.sin(x) for x in my_list_001]
[0.0, 0.8414709848078965, 0.9092974268256817, 0.1411200080598672]
# create sized list
sized_list = [1] * 4
# show the result
sized_list
[1, 1, 1, 1]
# list from 
>>> chars = ''.join(map(chr, range(32, 1048)))
>>> list(chars)
[' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5',
 '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', '`', 'a',
 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
 'x', 'y', 'z', '{', '|', '}', '~', '\x7f', '\x80', '\x81', '\x82', '\x83', '\x84', '\x85', '\x86', '\x87', 
'\x88', '\x89', '\x8a', '\x8b', '\x8c', '\x8d', '\x8e', '\x8f', '\x90', '\x91', '\x92', '\x93', '\x94', '\x95',
 '\x96', '\x97', '\x98', '\x99', '\x9a', '\x9b', '\x9c', '\x9d', '\x9e', '\x9f', '\xa0', '¡', '¢', '£', '¤', '¥',
 '¦', '§', '¨', '©', 'ª', '«', '¬', '\xad', '®', '¯', '°', '±', '²', '³', '´', 'µ', '¶', '·', '¸', '¹', 'º', '»',
 '¼', '½', '¾', '¿', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò',
 'Ó', 'Ô', 'Õ', 'Ö', '×', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é',
 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ', 'Ā',
 'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', 'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė',
 'Ę', 'ę', 'Ě', 'ě', 'Ĝ', 'ĝ', 'Ğ', 'ğ', 'Ġ', 'ġ', 'Ģ', 'ģ', 'Ĥ', 'ĥ', 'Ħ', 'ħ', 'Ĩ', 'ĩ', 'Ī', 'ī', 'Ĭ', 'ĭ', 'Į',
 'į', 'İ', 'ı', 'IJ', 'ij', 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