Create List from Elements of Tuples with Exclusion - python

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]

Related

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]

Using List Comprehensions to create multiple SubList in a list

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.

Creating a tuple with n entries for n lists

If I have a function that creates a random number of lists and I want the final result to return a tuple containing those lists - is it at all possible to do this?
Since tuples are immutable and the number of elements in a tuple cannot be changed once it has been created, then how can one be automatically generated which contains n entries for n lists?
Unlike lists, expanding the entries of a tuple can't simply be changed with a command like .append().
EDIT: It just dawned on me that I could create a tuple with a really large number of entries e.g. tuple = (a,b,c,d,e,f,g,h, ... to infinity) to cover n lists and then pass on the values of the tuple, which contain a list, to a new tuple with the exact number of n entries but this seems like a really inefficient method.
Create a list of lists, then pass that list to the built in tuple() function:
my_lists = [[1, 2], [5, 7], [8, 9]]
my_tuple = tuple(my_lists)
print(my_tuple)
Output
([1, 2], [5, 7], [8, 9])

How to pick the smallest value in a list when an iterative process is applied to the list

I need to find the smallest value in a series of lists. I understand the code for the smallest value in just one list:
>>> x = [1, 2, 3, 4, 5]
>>> print (min(x))
1
Simple enough. However, I would like to know if there is a way to write code that finds the smallest value for each list I have without stopping and adjusting the code (whether by an iterative process or some other means). Any help would be greatly appreciated. Thanks in advance!
First, make a list of lists out of your separate lists. For example, if you have lists A, B and C, the list of lists would be [A,B,C].
Now, to get a list of all the minimum values for each list in a list of lists lst:
[min(x) for x in lst]
To get the global minimum:
min(x for sublist in lst for x in sublist)
Demo:
>>> lst
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> [min(x) for x in lst]
[1, 4, 7]
>>> min(x for sublist in lst for x in sublist)
1
Edit in reaction to OP's comment:
I just want the minimum value for each list. I don't need to compile all of the minimum values together into a new list
If you just want to print the minimum values for each list A, B, C, ... , you can do:
for lst in (A,B,C): # put as many lists as you like into the parentheses
print(min(lst))
Edit in reaction to
I am only accessing one list (in this case, the values are well depths) at a time. [..] What I would like to know is how to write a code that finds the smallest value in a list given that the iterator essentially changes the values in said list.
Just print(min(lst)) each time lst has changed.
Assuming you've got a list of lists, this should do it:
minimums = [min(l) for l in lists]

Sorting a list of lists by the index of a sublist in python

I'm in the middle of a larger script and need to order a list by the index of the sub lists. My root list contains sub-lists of numbers corresponding to [Latitude, Longitude, Elevation, Distance]. I need to sort the root list by Distance in the sub lists. Any ideas?
you can use an operator.itemgetter to sort the list based on an element of your list:
import operator
lst = [...]
lst.sort(key=operator.itemgetter(3))
You need to say explicitly what is the value you want the list to be sorted by; in your case this 4th element of each element of your list. One way you can do it is to use key keyword parameter of the sort method and pass to it a function (lambda) which extracts that element:
>>> l = [[1,2,3,3],[1,2,3,2],[1,2,3,1]]
>>> l.sort(key=lambda s: s[3])
>>> l
[[1, 2, 3, 1], [1, 2, 3, 2], [1, 2, 3, 3]]
>>>

Categories