Python: Adding integer elements of a nested list to a list

Python: Adding integer elements of a nested list to a list

append nested list python
extract element from nested list python
nested list input in python
nested list comprehension python
python how to create nested list
add element to list python
how to fetch particular column from nested list in python ?
python list of lists example

So, I have two lists whose integer elements need to be added.

nested_lst_1 = [[6],[7],[8,9]]
lst = [1,2,3]

I need to add them such that every element in the nested list, will be added to its corresponding integer in 'lst' to obtain another nested list.

nested_list_2 = [[6 + 1],[7 + 2],[8 + 3,9 + 3]]

or

nested_list_2 = [[7],[9],[11,12]]

Then, I need to use the integers from nested_list_1 and nested_list_2 as indices to extract a substring from a string.

nested_list_1 = [[6],[7],[8,9]]    *obtained above*
nested_list_2 = [[7],[9],[11,12]]  *obtained above*
string = 'AGTCATCGTACGATCATCGAAGCTAGCAGCATGAC'

string[6:7] = 'CG'
string[7:9] = 'GTA'
string[8:11] = 'TACG'
string[9:12] = 'ACGA'

Then, I need to create a nested list of the substrings obtained:

nested_list_substrings = [['CG'],['GTA'],['TACG','ACGA']]

Finally, I need to use these substrings as key values in a dictionary which also possesses keys of type string.

keys = ['GG', 'GTT', 'TCGG']
nested_list_substrings = [['CG'],['GTA'],['TACG','ACGA']]

DNA_mutDNA = {'GG':['CG'], 'GTT':['GTA'], 'TCGG':['TACG','ACGA']}

I understand that this is a multi-step problem, but if you could assist in any way, I really appreciate it.


Assuming you don't need the intermediate variables, you can do all this with a dictionary comprehension:

a = [[6],[7],[8,9]]
b = [1,2,3]
keys = ['GG', 'GTT', 'TCGG']
s = 'AGTCATCGTACGATCATCGAAGCTAGCAGCATGAC'

DNA_mutDNA = {k: [s[start:start+length+1] for start in starts]
               for k, starts, length in zip(keys, a, b)}

How to split a list in half in Python, How do I add elements to a nested list in Python? Python. # List of lists listOfElems2D = [ [1,2,3,45,6,7], [22,33,44,55], [11,13,14,15] ] # Iterate over the list and add the size of all internal lists count = 0 for listElem in listOfElems2D: count += len (listElem) print ('Total Number of elements : ', count) 1. 2.


You can produce the substring list directly with a nested list comprehension, nested_lst_2 isn't necessary.

nested_lst_1 = [[6],[7],[8,9]]
lst = [1,2,3]
string = 'AGTCATCGTACGATCATCGAAGCTAGCAGCATGAC'
keys = ['GG', 'GTT', 'TCGG']

substrings = [[string[v:i+v+1] for v in u] for i, u in zip(lst, nested_lst_1)]
print(substrings)

DNA_mutDNA = dict(zip(keys, substrings))
print(DNA_mutDNA)

output

[['CG'], ['GTA'], ['TACG', 'ACGA']]
{'GG': ['CG'], 'GTT': ['GTA'], 'TCGG': ['TACG', 'ACGA']}

How to convert a nested list into a one-dimensional list in Python , How do you split a list in a list Python? Given a list of numbers, write a Python program to find the sum of all the elements in the list. Check out this Author's contributed articles. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org.


In[2]: nested_lst_1 = [[6],[7],[8,9]]
  ...: lst = [1,2,3]
  ...: string = 'AGTCATCGTACGATCATCGAAGCTAGCAGCATGAC'
  ...: keys = ['GG', 'GTT', 'TCGG']
In[3]: nested_lst_2 = [[elem + b for elem in a] for a, b in zip(nested_lst_1, lst)]
In[4]: nested_list_substrings = []
  ...: for a, b in zip(nested_lst_1, nested_lst_2):
  ...:     nested_list_substrings.append([string[c:d + 1] for c, d in zip(a, b)])
  ...: 
In[5]: {k: v for k, v in zip(keys, nested_list_substrings)}
Out[5]: {'GG': ['CG'], 'GTT': ['GTA'], 'TCGG': ['TACG', 'ACGA']}

How do you assign a list to a list in Python? Python indexing list elements. Elements in a Python list can be accessed by their index. Index numbers are integers; they start from zero. Indexes can be negative; negative indexes refer to elements from the end of the list. The first item in a list has index 0, the last item has -1.


Surely not the most readable way to do it, here is a bit of functional style fun:

nested_lst_1 = [[6], [7], [8,9]]
lst = [1, 2, 3]
nested_lst_2 = list(map(
    list,
    map(map, map(lambda n: (lambda x: n+x), lst), nested_lst_1)))
nested_lst_2

Result looks as expected:

[[7], [9], [11, 12]]

Then:

from itertools import starmap
from operator import itemgetter

make_slices = lambda l1, l2: starmap(slice, zip(l1, map(lambda n: n+1, l2)))
string = 'AGTCATCGTACGATCATCGAAGCTAGCAGCATGAC'
get_slice = lambda s: itemgetter(s)(string)
nested_list_substrings = list(map(
    lambda slices: list(map(get_slice, slices)),
    starmap(make_slices, zip(nested_lst_1, nested_lst_2))))
nested_list_substrings

Result:

[['CG'], ['GTA'], ['TACG', 'ACGA']]

And finally:

keys = ['GG', 'GTT', 'TCGG']
DNA_mutDNA = dict(zip(keys, nested_list_substrings))
DNA_mutDNA

Final result:

{'GG': ['CG'], 'GTT': ['GTA'], 'TCGG': ['TACG', 'ACGA']}

How do I make a nested list one list in Python? This is most generic method that strikes any programmer while performing this kind of operation. Just looping over whole list and convert each of the string of list to int by type casting. # Python3 code to demonstrate. # converting list of strings to int. # using naive method. # initializing list. test_list = ['1', '4', '3', '6', '7']


Add items to a Nested list. To add new values to the end of the nested list, use append() method. When you want to insert an item at a specific position in a nested list, use insert() method. You can merge one list into another by using extend() method. What is Python Nested List? A list can contain any sort object, even another list (sublist), which in turn can contain sublists themselves, and so on. This is known as nested list. You can use them to arrange data into hierarchical structures. Create a Nested List. A nested list is created by placing a comma-separated sequence of sublists.


This section will show how elements are added to a Python list. adding.py. #!/usr/​bin/env typeerror.py Message: list indices must be integers, not str. This is example In the example, we have three nested lists having two elements each. Elements can be added to the List by using built-in append() function. Only one element at a time can be added to the list by using append() method, for addition of multiple elements with the append() method, loops are used. Tuples can also be added to the List with the use of append method because tuples are immutable.


The values that make up a list are called its elements, or its items. The following list contains a string, a float, an integer, and (amazingly) another list: Using this produces a more elegant version of the nested loop program we previously And we can add elements to a list by squeezing them into an empty slice at the  In Python programming, a list is created by placing all the items (elements) inside square brackets [], separated by commas. It can have any number of items and they may be of different types (integer, float, string etc.). A list can also have another list as an item. This is called a nested list.