Add a number in the second element of each sublist

python get first element of list of lists
nested lists
extract element from nested list python
python sublist
for loop sublist python
python all sublists of a list
get first item from list of lists
find element in sublist python

Lets say I have:

a = [ [1,2], [2,9], [3,7] ]

I edited the second value of each list

seconds = [a[1] for num in a]
i = 0

while i <= len(seconds):
    seconds[i] += 3

How do I update the values to get this

a = [ [1,5], [2,12], [3,10] ]

Instead of first saving seconds and then modifying, you could do it all in one:

a = [ [1,2], [2,9], [3,7] ]

for item in a:
    item[1] += 3

Python, Python - Multiply K to every Nth element · Python | Append Odd element twice. Smitha Dinesh Semwal. Check out this Author's contributed articles. Python | Sort a list according to the second element in sublist In this article, we will learn how to sort any list, according to the second element of the sublist present within the main list. We will see two methods of doing this.

You could use a list comprehension, with extended iterable unpacking in order to only update the second element:

l = [ [1,2], [2,9], [3,7] ]
[[a, b + 3, *c] for a,b,*c in l]
[[1, 5], [2, 12], [3, 10]]

Which updates the second element of every sublist regardless of its length:

l = [ [1,2,5,10], [2,9,7,2], [3,7,1,5] ]
[[a, b + 3, *c] for a,b,*c in l]
[[1, 5, 5, 10], [2, 12, 7, 2], [3, 10, 1, 5]]

Note: This assumes that each sublist contains at least 2 elements

Python, Python - Sublist Maximum in custom sliced List · Python | Insert Nth element to Kth element in other list · Python | Slicing list from Kth element to last element  Note that each row corresponds to a sublist in the nested list: Use [ [ ] ] , the short form of the Part function, to get the second row: From row 2, get element 3:

Just a simple list comprehension would suffice:

a_2= [[each[0],each[1]+3] for each in a]

Printing a_2 would result in:

[[1, 5], [2, 12], [3, 10]]

Learning Perl 6: Keeping the Easy, Hard, and Impossible Within Reach, Smith Jon Smithers Change the sort to work only on the second element of each sublist: If they are the same, add another comparison with the logical or: my  Python | Add the occurrence of each number as sublists; Python | Replace elements in second list with index of same element in first list; Python | Subtract two list elements if element in first list is greater; Python | Remove sublists that are present in another sublist

Analysis of Algorithms: An Active Learning Approach, On the second pass, it could deal with groups of four elements each. The process repeats, increasing the number of elements per sublist and, therefore, decreasing list the elements to be put into order N the number of elements in the list 5. Python | Multiply each element in a sublist by its index Given a list of lists, the task is to multiply each element in a sublist by its index and return a summed list. Given below are a few methods to solve the problem.

How to find a specific element of a nested list, This assumes that each Key (each first element of a pair) are unique, but Given that you only want the second element, and there are no repeats, you could  1- Take first node of second list. 2- Start matching the first list from this first node. 3- If whole lists match return true. 4- Else break and take first list to the first node again. 5- And take second list to its second node. 6- Repeat these steps until any of linked lists becomes empty. 7- If first list becomes empty then list found else not.

Programming Language Pragmatics, avoid evaluating its second argument; you will need to learn how to deÞne macros Write insert and find routines that manipulate binary search trees in Scheme The Þrst element of each sublist should be the nonterminal; the remaining ("expr" (("(" "id" "number") ("term" "term_tail"))) 10.19 ("term" (("(" "id" " number")  There are two constructors for lists. The function list takes any number of arguments and returns a list with those arguments as its elements. > (list (+ 2 3) 'squash (= 2 2) (list 4 5) remainder 'zucchini) (5 SQUASH #T (4 5) #<PROCEDURE> ZUCCHINI) The other constructor, cons, is used when you already have a list and you want to add one new

  • Where is the "string"?
  • Your title does not match your question ;)
  • The OP didn't mention anywhere that the sublists would always be of size 2.
  • True @MuhammadAhmad, updated the answer
  • and you should also mention that this solution would only work if there are at least two elements in every sublist.
  • Why would you want to extract only the second element otherwise? Besides that would only work catching an exception if it doesn't exist, but dont think OP needs this, plus the other solutions don't add this either...
  • Yes, I'm not saying that your answer is wrong, I actually gave it a +1, but I was just saying that its good to mention that, because there's no fallback in that case.
  • Not valid Python code. NameError: name 'each' is not defined.
  • You will not get a NameError if you did what i just showed. I tried before posting my solution here.
  • @meowgoesthedog Both [[each[0],each[1]+3] for each[0],each[1] in a] and [[each[0],each[1]+3] for each in a] work. Realize that I gain nothing by making something up!
  • a_2= [[each[0],each[1]+3] for each[0],each[1] in a] doesn't work. Try running again in a fresh terminal/editor from scratch
  • I'm sure that you haven't made anything up, but you must have accidentally defined each before this line.