## DEV Community is a community of 731,460 amazing developers

We're a place where coders share, stay up-to-date and grow their careers. Aatmaj

Posted on

# 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:
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
['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!
Error!
Error!
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.