Using List Comprehensions to create multiple SubList in a list - python

I have a list:
list1 = [0,2,4,6]
Here I want to add number 2 to the list1 and create a sublist in new list , and create another sublist using the first sublist to create new sublist .
I want to create a new list from list1 with the help of list comprehentions only and that too in single line .
newList = [[2,4,6,8],[4,6,8,10],[6,8,10,12]]
For example :
newList = [a+2 for a in list1]
but with this code I am able to create only 1 list inside newList.but I want to create 3 sublist accordingly in newList using list Comprehensions only.

Given
list1 = [0,2,5,6]
you can create 3 lists inside a list using 2 nested list comprehensions:
newlist = [[x+2*(i+1) for x in list1] for i in range(3)]
the result
[[2, 4, 7, 8], [4, 6, 9, 10], [6, 8, 11, 12]]
is different from the one in the question, but Jim edited it in the meanwhile (for correctness), and I personally prefer that the input list has no internal logic (else we could be tempted to only use range to solve that).
Note that we can do it with list comprehension only because we can deduce the sublists from the initial input list. It's not possible in general to compute current element from previous one using list comprehension. You have to switch to a "classical" for loop in that case.

Related

create a dynamic list of list from a list of list using random

I'm trying to convert a list of list into a list of list with random values with some restrictions.
For example, I have the following list of lists.
a= [[10],[8],[4]] #this list will tell us how many elements/items should be in each list of list. For example in our new list, the first list will have 10 elements/items and so on.
b=[[15],[10],[5]] #this list will be used to determine the maximum number that each element can be. For example in our new list, since it can only have 10 elements/items we can only reach up to and including number 15. This process is done at random.
I need a new list a to look like the lists below. Each item in the list must be unique (so no repeating values is allowed)
new_list=[[1,2,4,5,8,9,10,11,12,13],[3,4,5,6,7,8,9,10],[1,2,4,5]] # sample output 1
new_list=[[5,6,7,8,9,10,11,12,13,14,15],[1,2,3,4,5,6,7,8],[2,3,4,5]] # sample output 2
This is what I have so far (MWE)
import random
a=[[10],[8],[4]] # list that informs the number of items in each list of list.
b=[[15],[10],[5]] # the restriction list.
new_list=[] #my final list
for values in a:
new_list.append(values[0])
new_list=[[item] for item in new_list]
print(new_list)
My problem is how do i convert the first list in my new_list into a random list using list b.
I hope this makes sense.
You can use a list comprehension with zip:
new_list = [sorted(random.sample(range(1, j[0]+1), i[0])) for i,j in zip(a,b)]
Example output:
[[1, 2, 4, 7, 8, 10, 11, 12, 13, 14], [1, 3, 4, 5, 7, 8, 9, 10], [1, 2, 3, 4]]
Note that it would be easier to store your input as:
a = [10, 8, 4]
b = [15, 10, 5]
Then you wouldn't need to slice i/j with [0]

Collecting first item of a list in a list of lists

I have a list that looks like the following one:
my_list = [[[(2,3,4,5)]],[[8,2,4,2]],[[9,0,0,0]]]
Now, I want to find a way to make a new list zero_list whose elements will be the 0th entries of each element in my_list. That is
zero_list = [2, 8, 9]
How could I make a for loop for iterating the 0th element of a list whose elements are lists themselves? Of course in my real example I have a much bigger such list with thousands of entries who are lists themselves.
P.S. I understand this is probably an easy question but I could not figure it out.
For any depth list you can use this recursive function
def get_first(seq):
if isinstance(seq, (tuple, list)):
return get_first(seq[0])
return seq
def get_zero_list(seq):
return [get_first(i) for i in seq]
my_list = [[[[[[[[(2,3,4,5)]]]]]]],[[8,2,4,2]],[[9,0,0,0]]]
print(get_zero_list(my_list)) # [2, 8, 9]
my_list = [[[[[[[[(2,3,4,5)]]]]]]],[[[[[[[('first', 3)]]]], 2, 3],2,4,2]],[[([['a', 2]]),0,0,0]]]
print(get_zero_list(my_list)) # [2, 'first', 'a']
Zero_list = [ items[0] for sublist in my_list for items in sublist]

Finding and replacing within a 'grouped' list

Is there a way to do a find and replace for lists that are sorted into groups?
Scenario:
my_list = [[1,5],[3,6],[-1,9]]
I want to replace all the values that are 1 or 3 to be replaced with 11 such that output is:
my_list = [[11,5],[11,6],[-1,9]]
I have been able to do the find replace by creating 3 variables and adding it such that it is one big list however I still want to retain the same form thus I am wondering how to do it while it's still in that form?
The alternative to the list comprehension solution would be modifying the original list with:
for group in my_list:
for i, x in enumerate(group):
if x in {1, 3}:
group[i] = 11
This would be the best option if your lists contain a large number of elements.
You could achieve this with a nested list comprehension such as:
my_list = [[y if y not in [1, 3] else 11 for y in x] for x in my_list]
This retains the nested list structure and replaces any 1 or 3 with 11. Output is:
[[11, 5], [11, 6], [-1, 9]]

Create List from Elements of Tuples with Exclusion

I want to make a list of items from the elements of tuples in a list such that those elements don't belong to some other list, and I know that each tuple contains one element from the list I don't want it to belong to and one element that's not in that list. For example, with
tuples = [(2,1), (1,4), (1,7), (3,10), (4,3)]
exclude = [1, 3]
I am looking to create the list
[2, 4, 7, 10]
This is easy enough to accomplish in a clumsy for loop, but it seems like there's a more pythonic way using some function or list comprehension. Any ideas?
Didn't actually understand the question. Assuming this may be you want
>>>list(set([j for i in tuples for j in i if not j in exclude]))
[2, 4, 10, 7]
Assuming your requirement is to convert list of tuples to a list and then getting unique elements in the list, exclusing the list exclude and then sorting them.
from itertools import chain
tuples_final = sorted(list(set(chain(*tuples))-set(exclude)))
You forgot a 4 in your example, the code will return :
>>>[num for tup in tuples for num in tup if num not in exclude]
[2, 4, 7, 10, 4]

Concatenate List Object Name with a Number and Retain the List Python

I'm using python 2.7 I'm trying to figure out a way to change the names of my lists automatically.
Let me explain i have multiple lists
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 9, 3]
list3 = [8, 4, 3, 2, 1]
I would like to call the lists in a loop to determine which lists contain or do not contain a particular number.
My first thought was
x = "list" + str(i) # (where i iterates in the loop)
print x
However, using the above code only gave me the string "list1"(when i=1).
What I want is to be able to call the list that is named list1 and use the .count() operator to determine whether or not the number exists if it doesn't i want to call the next list until I'm out of lists(there will eventually be up to 30 lists).
Thanks,
Ryan
You shouldn't approach it like this. Put your lists in a container to iterate over them instead:
In [5]: for l in (list1, list2, list3):
...: print l.count(2)
...:
1
0
1
What you could do in a real-life use case is create a list of lists and fill it dynamically.
Then to get the first list that contains a given number, you could do:
In [6]: lists = [list1, list2, list3]
In [7]: next(l for l in lists if 9 in l)
Out[7]: [4, 5, 9, 3]
put the list in dict:
list1 = [1,2.4]
list2 = [2,5,6]
dlist = {1:list1,2:list2}
for k in dlist:
print dlist[k]

Categories