My code:
list=[["Uno","Dos"],[1,2],["Tres","Cuatro"],[3,4]]
print(list)
list[4][0].insert(A)
New_list=[[],[],[],[]]
A=list[0][0] str(list[1][0])
B=(list[0][0] str(list[1][0]))[::-1]
C=list[0][1] str(list[1][1])
D=(list[0][1] str(list[1][1]))[::-1]
E=list[2][0] str(list[3][0])
F=(list[2][0] str(list[3][0]))[::-1]
G=list[2][1] str(list[3][1])
H=(list[2][1] str(list[3][1]))[::-1]
New_list[0][0].append(A)
print(New_list)
My expectation:
[["Uno1","1onU"],["Dos2","2soD"],["Tres3","3serT"],["Cuatro4","4ortauC"]]
I've tried to use append and insert but every time I get an error message; usually "list index is out of range"
How can I add my values to New_list?
CodePudding user response:
append
appends a value to your list inline. list[4][0]
gives an error, because that value does not exist yet. With insert
you can add entries to your list and specify the location as well where you want the entry to be insert, whereas append
always attaches to the end of the list.
Example:
l = []
l.append("a")
print(l)
# ['a']
l.append(["b", "c"])
print(l)
# ['a', ['b', 'c']]
l.insert(0, "d")
print(l)
# ['d', 'a', ['b', 'c']]
CodePudding user response:
Answering your question "How do I append values to make a multi-dimentional list?" You can append a list to a list, making it multi-dimensional. Small example:
my_list = []
my_list.append([1, 2, 3])
my_list.append([4, 5, 6])
my_list.append([7, 8, 9])
print(my_list)
# prints my_list (formatted nicely):
# [
# [1,2,3],
# [4,5,6],
# [7,8,9]
# ]
You are also trying to access out of bounds of your list on line 3, causing an error. Remember index starts at 0 for lists.
CodePudding user response:
Use loops to perform similar operations through the list, rather than assigning separate variables for each list element.
old_list = [["Uno","Dos"],[1,2],["Tres","Cuatro"],[3,4]]
new_list = []
for i in range(0, len(old_list), 2):
num_strings = old_list[i]
nums = old_list[i 1]
for s, num in zip(num_strings, nums):
new_list.append([f'{s}{num}', f'{num}{s[::-1]}'])
print(new_list)
CodePudding user response:
got something for you :
list=[["Uno","Dos"],[1,2],["Tres","Cuatro"],[3,4]]
A = list[0][0] str(list[1][0])
reverse_A = A[::-1]
elemA = [A, reverse_A]
B = list[0][1] str(list[1][1])
reverse_B = B[::-1]
elemB = [B, reverse_B]
C = list[2][0] str(list[3][0])
reverse_C = C[::-1]
elemC = [C, reverse_C]
D = list[2][1] str(list[3][1])
reverse_D = D[::-1]
elemD = [D, reverse_D]
new_list = [elemA, elemB, elemC, elemD]
print(new_list)
CodePudding user response:
I think trying to do it by hand is little bit harder as when list grows. U can wrap it inside a FOR loop as below:
for i in range(0,len(list),2):
str1=list[i][0] str(list[i 1][0])
str2=list[i][1] str(list[i 1][1])
New_list.append([str1,str1[::-1]])
New_list.append([str2,str2[::-1]])
This one can adapt to bigger lists automatically. I hope I've given you u the answer you were looking for
CodePudding user response:
First, you cannot use list as an alias. Secondly, lists start numbering at 0, none of your lists have an element at list[4]. That would be a fifth element. Try this.
lista = [['Uno','Dos'],[1,2],['Tres','Cuatro'],[3,4]]
A = lista[0][0] str(lista[1][0])
B = lista[0][1] str(lista[1][1])
C = lista[2][0] str(lista[3][0])
D = lista[2][1] str(lista[3][1])
Lists = A, B, C, D
New_List = []
for lisx in Lists:
New_List.append((lisx, lisx[::-1]))