Unknown number of nested list in python 3 - python

How to create a nested list ?
For example there is a list lines[None]*int(nl) where nl is number of lines, input taken from user, and in lines[] I want to create multiple lists which would hold numbers for different lines.

the below code shows you how to add a list to a list. i am assuming this is what you wanted
list_example = []
# this will be your base list
print (list_example)
#now lets add a sub list
list_example.append([])
print (list_example)
#now lets add a list to that list
list_example[0].append([])

Related

Putting Result of Multiplication in a List

I am trying to put a series of multiplications inside a list, I am using the code below:
listx = []
for i in range (2):
list = [(3*i)]
listx.append(list)
The problem is that this will put the two results inside two separate lists inside a lists, I just wants the floats to be inside the first list.
listx = []
for i in range (2):
listx.append(3*i)
Just use this one. There is no need to create another list for storing the result. You created another list to store the value and appended that list into your listx
You can also use list comprehensions. Basically it's the same with for cycles but it's much shorter and for simpler operations like yours it's easier to read.
listx = [i*3 for i in range(2)]
This should produce a single list with values multiplied by 3 as integers

concatenation result add two lists

Create two lists by taking inputs from the user. First input is number of elements and second input is values in the list. Each list should only contain string as its member elements. Create a resultant list such that this list contains the concatenation result of elements of first list with each element of second list
Create two lists by taking inputs from the user. First input is number of elements and second input is values in the list. Each list should only contain string as its member elements. Create a resultant list such that this list contains the concatenation result of elements of first list with each element of second list.
print(list(map(''.join,zip(input('value: ')*int(input('number of elements: ')),input('value: ')*int(input('number of elements: '))))))
creates two lists
concatenates corresponding elements
returns one list

change values in last two lists of lists

I am looking for a solution to change the last two lists in a list of lists. The number of lists inside the list is variable. The change of the values depends on every time the last two lists.
list_of_colors=[['red','red','red','red'],['red','red','red','red'],
['red','red','red','red'], ['red','red','red','red']]
I expect the second to the last list changes completely from 'red' to 'lightgrey' and in the last list, only the last two from 'red' into 'lightgrey' - like this:
list_of_colors=[['red','red','red','red'],['red','red','red','red'],
['lightgrey','lightgrey','lightgrey','lightgrey'], ['red','red','lightgrey','lightgrey']]
This list of lists is to color a plotly table - Thanks for the help
For a more dynamic solution, i.e. in case the lists don't have a fixed length, you can try this:
list_of_colors[-2] = ['lightgrey'] * len(list_of_colors[-2])
last_n = 2
list_of_colors[-1][-last_n:] = ['lightgrey'] * last_n
In the last_n variable, I have specified the number of elements you wish to change, of the last list.
If I understood your question correctly, the parent list which contains the lists can have variable number of lists inside it, and you specifically know what values you require inside the last two lists, Then, this should work:
list_of_colors[-1] = ['lightgrey','lightgrey','lightgrey','lightgrey']
list_of_colors[-2] = ['red','red','lightgrey','lightgrey']

Combining three different list in python

Thank you for looking at my issue.
I'm trying to compare cells from three csv files to make sure they are exactly the same info. the cells in the csv can contain names, dates or ID numbers. All have to match.
compile = []
for a in Treader,Vreader,Dreader:
for b in a:
compile.append(b[0])
However, the number of variables will fluctuate and I don't want to keep adding index splicing every time. see "complie.append(b[0])" . The question now what way can I construct this to give me a random amount of variables and random number of indexes based on the length "len" of the original list. can i use the range function for that? not sure how i can create something like this.
The current question I have is
List = [[sally,john,jim], [sally,john,jim], [sally,john,jim]]
If I have the list above how could I get it to show
List =[sally,sally,sally]
List1 = [john,john,john]
List2 = [jim,jim,jim]
Also I want to be able to come up with unlimited number of list based on the length of this list that is inside the list. In this case its 3 for three names.
Some of my list has 30 some has 5 so its important I can assign it without having to type list1 to list 30 and manually assign each one.
you may use:
compile = list(zip(Treader,Vreader,Dreader))
this will create a list of tuples, a tuple will have like (sally,john,jim)
after your edit
you may use:
list(zip(*List))
output:
[('sally', 'sally', 'sally'), ('john', 'john', 'john'), ('jim', 'jim', 'jim')]

How to append elements inside a list as a list?

I have two lists named queries_fetcher_list and reftable_column_name.
Now I need to perform the operation of taking an element from a and b, and make it as a tuple with zip().
This is the query for doing that:
some_list = []
for i in range (len(reftable_column_name)):
for row in queries_fetcher_list[i]:
some_list.append(dict(zip(reftable_column_name[i], row)))
Now I need that result to be append like this:
[[{first element}], [{second element}]]
What I need to do now is every time when a zip operation is performed that element has to append as a separate list, i.e. list inside a list, like this:
a = []
a = [['one'], ['two'], ['three']]
queries_fetcher_list contains a list of data that are retrived from an MySQL query retrieved with cursor.fetchall().
reftable_column_name is a list contains the column names of a table retrived with cursor.description().
some_list.append([dict(zip(reftable_column_name[i], row))])
will append single-element lists containing your dict.
define a list and everytime when elements got append into it free it, means empty it,now you can achieve what you want,eveytime when an element is appended into a list the list will be flushed and becomes an empty list but make sure to add your elements like this "list(your_element)".

Categories

Resources