- #1
Kolika28
- 146
- 28
I have a list containing several lists with two elements each. I want to multiply the last element in the inner lists with a number x, and then print the new list. How do I do this?
Last edited:
jedishrfu said:Is this a homework assignment? If so what is the actual problem you're trying to solve.
Start by making a for loop to iterate through the list and within the for loop work with the inner list.
for pair in mylist:
pair[1] *= 5
mylist=[[2000, 28],
[2001, 32],
[2002, 36],
[2003, 39]]
newlist=list(mylist)
number=len(newlist)
for i in range(number):
newlist[i][-1]=round(newlist[i][-1]*5)
print(newlist)
print(mylist)
mylist=[m[:-1]+[5*m[-1]] for m in mylist]
m
becomes each element (i.e., each sub-list) in mylist
in turn. m[:-1]
is every element in m
except the last, and m[-1]
is the last element. Technically this structure is called a generator, because it's a process for generating modified elements from mylist
. Wrapping the whole thing in square brackets gets python to execute the generator and return the result as a list.I think the problem is that list(mylist) makes a copy of your list, but it still contains the same sublists - you didn't copy those. So you need to copyKolika28 said:General code:
Code:mylist=[[2000, 28], [2001, 32], [2002, 36], [2003, 39]] newlist=list(mylist) number=len(newlist) for i in range(number): newlist[i][-1]=round(newlist[i][-1]*5) print(newlist) print(mylist)
So I have almost figured it out now. I only have one problem left: when I'm working with newlist, the orginal also gets modified. My textbook tells me that I have to use the function list() to make a copy of the orginal, but it doesn't work.
newlist[i]
before modifying it.Hmm, I'm not quite sure if I understand what you mean...Ibix said:I think the problem is that list(mylist) makes a copy of your list, but it still contains the same sublists - you didn't copy those. So you need to copynewlist[i]
before modifying it.
What @FactChecker says.Kolika28 said:Hmm, I'm not quite sure if I understand what you mean...
newlist = [[x, 5*y] for [x, y] in mylist]
Ohh, I see. Thank you for a good explanation!Ibix said:What @FactChecker says.
Lists in python don't, strictly speaking, contain their elements. Instead, they contain the memory addresses where their elements can be found. So copying the list doesn't copy the sub-lists, just the addresses, and modifying the sub-list "in" the copy modifies the sub-list "in" the original.
So you need to make your newlist from copies of each of the sublists in the original.
Edit: think of a list of the things in a drawer. Copying the list doesn't copy the contents of the drawer.
wle said:You can use a list comprehension if you want to create a new list without modifying the original one.
Python:newlist = [[x, 5*y] for [x, y] in mylist]
Kolika28 said:Ohh, I see. Thank you for a good explanation!Ibix said:What @FactChecker says.
Lists in python don't, strictly speaking, contain their elements. Instead, they contain the memory addresses where their elements can be found. So copying the list doesn't copy the sub-lists, just the addresses, and modifying the sub-list "in" the copy modifies the sub-list "in" the original.
So you need to make your newlist from copies of each of the sublists in the original.
Edit: think of a list of the things in a drawer. Copying the list doesn't copy the contents of the drawer.
>>> a = [1, 2, 3, 4, 5]
>>> b = a
>>> b[2] = 99
>>> a
[1, 2, 99, 4, 5]
Thanks, I really appreciate your help. I tried using the function copy.deepcopy(), and it worked out great!wle said:In case you don't know it already it's maybe worth adding that not only the contents of lists but variables in general are all references (a.k.a. pointers) in Python. This has a visible effect in code like the following:
This behaviour is also why the code I posted above with the pair variable correctly modifies your original list.Python:>>> a = [1, 2, 3, 4, 5] >>> b = a >>> b[2] = 99 >>> a [1, 2, 99, 4, 5]
In order to access elements in nested lists, you can use the indexing notation. For example, if you have a nested list called "my_list" with two lists inside, you can access the first element of the first list by using my_list[0][0], and the second element of the second list by using my_list[1][1].
The best way to multiply elements in nested lists in Python is by using nested for loops. This allows you to iterate through each list and access the elements individually, perform the multiplication, and store the results in a new list.
You can use the isinstance() function in Python to check the type of an element. This function takes two parameters: the element you want to check and the data type you want to check against. For example, isinstance(my_list[0][0], int) will return True if the first element of the first list in my_list is an integer.
Yes, list comprehensions can also be used to multiply elements in nested lists in Python. However, it may be more difficult to read and understand compared to using nested for loops.
After multiplying the elements in nested lists, you can simply use the print() function to print out the new list. You can also use a for loop to iterate through the new list and print each element individually.