Learning Python-Basic course: Day 16, Fractal lists and other questions

🤟WELCOME 👍 Today let us look at a few miscellaneous questions related to multidimensional list which use try-except😃

In case you haven't visited yesterday's blog, please do so. We have covered some really good questions based upon try-except

1) Alphabetical order of letters.

a=[]
for i in range(0,4):
   try:
    a.append(ord(input("Please enter a character ")))
   except:
    print("Error!")
a=sorted(a)
#The sorted() method sorts the list and returns the sorted list
# This is an inbuilt function to sort the list. You can also use insertion sort.
for i in range(0,len(a)):
    a[i]=chr(a[i])
print(a)

Output-

Please enter a character a
Please enter a character d
Please enter a character b
Please enter a character h
['a', 'b', 'd', 'h']

Note- The inbuilt sorted() method is so nice, that it will directly sort the values in alphabetical order even if we do not convert them into integers! Try removing the chr() and ord() functions and running the code.

Till now we hadn't handled errors for the input. However in this example, we handle errors for input values, example we can prevent errors f the user inputs more than one character.
Output-

Please enter a character 123
Error!
Please enter a character abc
Error!
Please enter a character -2
Error!
Please enter a character 1a
Error!
[]

2) Fractal lists.
We will now try to generate a fractal list. Fractal list is a multi- multidimensional list looks something like this-
[1, 2, [1, 2], [1, 2, [1, 2]], [1, 2, [1, 2], [1, 2, [1, 2]]]]
Got the pattern? Basically we must append the list into itself. So now let us try doing so...

a=[1,2]
for i in range(0,3):
    a.append(a)
print(a)

Output-

[1, 2, [...], [...], [...]]

Well, that didn't work. This is because the python syntax doesn't allow us to append to a list like this. We must use a temp variable to store the value.

a=[1,2]
for i in range(0,3):
    temp=a
    a.append(temp)
print(a)

Output

[1, 2, [...], [...], [...]]

Didn't work either! This is because when we assign temp=a and append temp,, then we are doing the same thing as before! The solution is using the copy() method.

a=[1,2]
for i in range(0,3):
    temp=a.copy()
    a.append(temp)
print(a)

Output-

[1, 2, [1, 2], [1, 2, [1, 2]], [1, 2, [1, 2], [1, 2, [1, 2]]]]

Exercise

1)- Write a program to find the length of the list in the fractal list. Answer

2) Write a program to reverse the lists in a list. Example

In: [[1,2,3],4,[5,6],[7,[8,9],10],[11,12,13,14],15]
Out: [[3, 2, 1], 4, [6, 5], [10, [8, 9], 7], [14, 13, 12, 11], 15]

Hint- use try except pass for non list values.

✌️So friends that's all for now. 😊 Hope you all are having fun.😎 Please let me know in the comment section below 👇. And don't forget to like the post if you did. 😍 I am open to any suggestions or doubts. 🤠 Just post in the comments below or gmail me. 😉
Thank you all👍
Star the Learning-Python repo made for this course!😃

🤫psst... follow me on dev.to and GitHub for updates...

17