Append a list that’s in another list - python

I’m trying to append a list that’s in another list.
import random
list1 = []
list2 = []
list3 = [list1, list2]
Index = random.choice(list3)
Index = list3.index(Index)
print(Index)
list3[Index].append(“Test”)
print(list3[Index])
I want it to add “test” to either list1 or list2 depending on the value of index.
For some reason, if I repeat the process twice, and the value of index changes, “Test” is added twice to the same list. How do I accomplish this?

random.choice returns one of the items in the given list, which in this case are the references to, rather than the indices of, list1 and list2.
You should append directly to the list returned by random.choice instead:
import random
list1 = []
list2 = []
list3 = [list1, list2]
lst= random.choice(list3)
lst.append('Test')
print(list3)
This can output:
[['Test'], []]

Related

How to return all item in list of lists that are not in other list of lists in Python?

I have 2 list of lists:
list1: [[1,2,3],[2,5],[6,7,4]]
list2: [[1,3],[2],[6,4],[9,0,3]]
I want to do few things:
Find every number that is in list 1 but is not in list 2, store it in a new list, and then append this new list to list2.
In our case: new_list = [5,7]
and then, we will add it to list2 = [[1,3],[2],[6,4],[9,0,3],[5,7]]
Then, I want to remove duplicates of numbers from each list1 and list 2:
In our case: list1 = [[1,3],[2,5],[6,7,4]], list2 = [[1],[2],[6,4],[9,0,3],[5,7]]
I have an implementation using For loops, but I need something more elegant for that.
Can you help me please find out a way?
Using Python, you can do the first part with a set comprehension:
list2.append(list({i for j in list1 for i in j}.difference({i for j in list2 for i in j})))

How to make a list of certain items from a different list

For example: list1 = [1t, 1r, 2t, 2r, 3t, 3r...., nt, nr]. How do I make a list list_t that has all t items from list1? I tried using the following for loop:
for i in list1[0:]:
list_t =[i.t]
But this only assigns the first item to list_t.
If your list has the same items repeated at the same time step, then:
list1 = ['1t', '1r', '2t', '2r', '3t', '3r']
# list[start:stop:step]
l2 = list1[0::2]
print(l2)
will solve your problem.
However, if what you mean is that you have a list of strings and you need to extract the strings with t on it, then you can just test if t is in the element, like so:
l2 = list()
for i in list1:
if 't' in i :
l2.append(i)
print(l2)

Introducing new variables using a given string

I have a list of names my_names = ["A","B","C"]
I have a function that can takes each name from a list and returns several lists of information for that name.
def my_function(name):
return list1, list2, list3
For each name list1, list2, list3 are different.
I want to write a function that wouldn't need any user input but would return a list with 3 lists inside.
def my_function():
for name in my_list:
# Part I don't know how to do
name_list = list1, list2, list3
# A_list for "A", B_list for "B", C_list for "C"
return A_list, B_list, C_list
The only thin I don't know is how to make python introduce new empty lists depending on the name in my_list
A dictionary is best, but you can have a list of lists. Just make a main list: name_list = [] then append each list to it: name_list.append(list1), etc. Then reference each list using its index, then the elements in that list with a secondary index. For example,
def my_function():
for name in my_list:
name_list = []
name_list.append(list1)
name_list.append(list2)
name_list.append(list3)
return name_list
Then if you want to access the second element in the first list from the returned function, you would do so like:
name_list[0][1]
It's hard to say more without knowing more about your problem, but this will work, it's just not optimal.
You can create a nested list with n numbers of sublists, where n is any given number of lists
n = 3
nested_list = [[]] * n
# returns [[],[],[]]
#function that takes n, n=3
def create_nested_list(n):
nested_list = [[]] * n
return nested_list
nested list = create_nested_list(n)
You can append items in the nested list's lists by indexing, for instancce nested_list[0]=["A"] will append the number "A" to the first sublist, nested_list[1]=["B"]to the second and nested_list[2]=["C"] to the third sublist, so nested_list = [["A"],["B"],["C"]]

compare two lists and print out unequal elements

I have two lists in the following format:
list1 = ['A','B','C','D']
list2 = [('A',1),('B',2),('C',3)]
I want to compare the two lists and print out a third list which will have those elements present in list1 but not in list2 and I want to compare only the list2[i][0] elements.
I tried the below code:
fin = [i for i in list1 if i not in list2]
But it prints all the elements in list1. I want the output in the above case to be :
fin = ['D']
Could somebody please suggest how to do that?
Also, I do not want to convert my 2D array list2 to 1D array.
Use the set difference.
set(list1) - set(i[0] for i in list2)
You can do this as well (you need to compare i with the first element of each tuple in list2):
fin = [i for i in list1 if i not in map(lambda(x,_):x,list2)]
How about nested comprehensions:
fin = [a for a in list1 if a not in [b for b,_ in list2]]

Python append adds an item to every variable

I'm iterating on a list and attempting to create sublists from its items. Every time I append to a variable, the value is added to every other variable that I have defined. I've stripped down the code substantially to illustrate.
item = 'things.separated.by.periods'.split('.')
list1 = list2 = []
i = item.pop(0)
print i
list1.append(i)
i = item.pop(0)
print i
list2.append(i)
print(item, list1, list2)
Returns:
things
separated
(['by', 'periods'], ['things', 'separated'], ['things', 'separated'])
What I expected:
things
separated
(['by', 'periods'], ['things'], ['separated'])
I think this might by answered here, but I'm not sure how to apply this fix to my circumstances. Thanks in advance!
The problem is the line
list1 = list2 = []
This makes list1 and list2 refer to the exact same list, so that if you append an item to one you also append it to the other. Change it to
list1 = []
list2 = []
list1 = list2 = []
You are setting list1 to be the exact same list as list2. Therefore, they basically mean the same thing.
To fix this, try something like this:
list1, list2 = [], []

Categories