Adding a value to a list of lists - python

I want to add a value to a list of lists.
For input of [[1,2],[2,3]]
I want output of [[2,3],[3,4]]
I can do it with loops:
list_of_lists = [[1,2],[2,3]]
output = []
for list in list_of_lists:
sub_output = []
for value in list:
sub_output.append(value+1)
output.append(sub_output)
print(output)
Can I do this with list comprehension?
If I do:
[value + 1 for list in list_of_lists for value in list]
It gives me [2,3,3,4]. Can I get it to keep the sublist format somehow?

Try...
[ [n + 1 for n in inner_list] for inner_list in list ]

Yeah, you need a nested comprehension:
[[item + 1 for item in list] for list in list_of_lists]
Another way would be to use map:
map(lambda l: map(lambda i: i + 1, l), list_of_lists)

You need to nest a comprehension into that comprehension. Unpack each sublist to make it easier.
[[a+1, b+1] for a,b in list_of_lists]

Related

List resolver with tuples

I have a list:
List = [('4022-a751',), ('0bfc-4d53',)]
And want to resolve it into the output below:
Output = ['4022-a751','0bfc-4d53']
You should read about List Comprehensions in Python
list_ = [('4022-a751',), ('0bfc-4d53',)]
res = [x for item in list_ for x in item]
Output
['4022-a751', '0bfc-4d53']
A tuple can be manipulated like an array with index.
input_arr = [('4022-a751',), ('0bfc-4d53',)]
output_arr = [a[0] for a in input_arr]
print(output_arr)
You can use this.
old_list= [('4022-a751',), ('0bfc-4d53',)]
new_list = [''.join(i) for i in old_list]
print(new_list)

filter a list based on values in another list

list1 = ['a','b','c','d']
list2 = [1,0,1,0]
Given two lists like the above, I would like to obtain a third list whose values are ['a','c']
In other words, I'd like the target list to be the values from list1 where the corresponding element in list2 is 1.
As noted in the comments:
[i for i, j in zip(list1, list2) if j] would work.
Alternatively, if you were looking for something not so advanced:
list3 = []
for i in range(len(list1)):
if list2[i] == 1:
list3.append(list1[i])
Use enumerate function on second list to include index, which can be used for the first list.
[list1[i] for i, item in enumerate(list2) if item]
Generators can be nice if you have very long lists:
def filterfunc(a, b, keyvalue=1):
return (x for i, x in enumerate(a) if b[i] == keyvalue)
To get the whole sequence:
list(filterfunc(a, b))

How to split a list of lists

So I want to split a list of lists.
the code is
myList = [['Sam has an apple,5,May 5'],['Amy has a pie,6,Mar 3'],['Yoo has a Football, 5 ,April 3']]
I tried use this:
for i in mylist:
i.split(",")
But it keeps give me error message
I want to get:
["Amy has a pie" , "6" , "Mar 3"] THis kinds of format
Here's how you do it. I used an inline for loop to iterate through each item and split them by comma.
myList = [item[0].split(",") for item in myList]
print(myList)
OR You can enumerate to iterate through the list normally, renaming items as you go:
for index, item in enumerate(myList):
myList[index] = myList[index][0].split(",")
print(myList)
OR You can create a new list as you iterate through with the improved value:
newList = []
for item in myList:
newList.append(item[0].split(","))
print(newList)
Split each string in sublist :)
new = []
for l in myList:
new.append([x.split(',') for x in l])
print new
It's because it's a list of lists. Your code is trying to split the sub-list, not a string. Simply:
for i in mylist:
i[0].split(",")
Another way would be:
list(map(lambda x: x[0].split(","), myList))

How to make a list with elements to list with list with only one element?

I have a list with n elements but i would like to convert it to a list which contains n list, and every list contains a single element.
a = ['1','2','3','4']
b = [['1'],['2'],['3'],['4']]
How can I make from a to b?
You can try list comprehension
b = [[i] for i in a]
You can use map:
b = list(map(lambda x: [x], a))
or a list comprehension:
b = [[i] for i in a]
You can iterate through the a list and append new list with the item to b list.
a = ['1','2','3','4']
b = []
for i in range(len(a)):
b.append([a[i]])
print(b)
This is basically the same solution as the one from JRodDynamite, but more readable for beginners.
Very Simple Solution Could be from generators
a = ['1','2','3','4']
b = [[item] for item in a]

How to take a simple list of integers and modify it to a list of lists?

I already have a list of integers:
lst = [120, 250, 310]
And after some calculations i need to append each new result to the integer i used. There is a for loop which encapsulates the whole thing. So after something like that:
for item in lst:
for ke, va in somedict.items():
if (item + va) in someotherdict:
lst = ?here is where i'm stuck
I need the result to be:
lst = [[120,110], [250,200], [310,330]]
in the next loop:
lst = [[120,110,150], [250,200,180], [310,330,99]]
etc... any ideas?
You can first convert your list of integers to a list of lists of integers:
for i in range(len(lst)):
lst[i] = [lst[i]]
Then you can append to each:
for itemlist in lst:
item = itemlist[0]
for ke, va in somedict.items():
if (item + va) in someotherdict:
itemlist.append(somevalue)

Categories