I'm very new in python. I just stuck in one problem.
I have two lists, like -
list1 = [1,2,3,4,5]
list2 = [3,6,8,9,1,2,4,0,5]
Now I have to check each value of list1 with that it is present in list2 or not. If all the value of list1 is present in list2 then update the list2 by removing extra values from list2. So list2 will be like -
list2 = [3,1,2,4,5]
Tried to do this with nested loop. but it is reading only 1st value. how can I do that?
One of the many options for performing the task
list1 = [1, 2, 3, 4, 5]
list2 = [3, 6, 8, 9, 1, 2, 4, 0, 5]
list2 = [num for num in list2 if num in list1 and set(list1).issubset(list2)]
Related
This question already has answers here:
How do I remove duplicates from a list, while preserving order?
(30 answers)
Closed 1 year ago.
I tried the following code but it doesn't seem to remove any duplicates
list2 = [element for element in list1 if element not in list2]
however,
for i in list1:
if i not in list2:
list2.append(i)
this code works perfectly fine, can anyone please let me know why is this the case?
It's producing an identical list as list2 contains no elements at run-time. What you'd want is this:
list1 = [1, 2, 3, 3, 5, 9, 6, 2, 8, 5, 2, 3, 5, 7, 3, 5, 8]
list2 = []
[list2.append(item) for item in list1 if item not in list2]
print(list2)
What is the difference of this two list in python:
list1 = [1,2,3,4,5,6,7,8,9]
list2 = [[[[1,2,3,4,5,6,7,8,9]]]]
When I use type(list1) and type(list2), all come with list , but when I try to make some deal such as:
Using list1:
new_total=[]
for i in range(0,len(list1),3):
b=list1[i:i+3]
print(len(b))
output:
9
6
3
Using list2:
for i in range(0,len(list2),3):
b=list2[i:i+3]
print(len(b))
output:
1
Well the elements within list 2 are the first element of the list within a list within a list.
So they are both of type list, however in the first you are printing the length of three indexed values hence 3.
In the second for loop you are printing the length of the inner list within a list, that only has one element in it (another list, which contains a list that contains the list of numbers within that)
Basically you have embedded the list of numbers 4 fold as the first element
within the original list
replying for only to clarify these reponses , just to help you to understand (as a friend) , i'll give some exemples, that may help you:
list1 = [1,2,3,4,5,6,7,8,9]
list2 = [[[[1,2,3,4,5,6,7,8,9]]]]
print (list2[0][0][0][0])
print (list2[0][0][0])
print (list2[0][0])
print (list2[0])
print (list2)
Output:
1
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[[1, 2, 3, 4, 5, 6, 7, 8, 9]]
[[[1, 2, 3, 4, 5, 6, 7, 8, 9]]]
[[[[1, 2, 3, 4, 5, 6, 7, 8, 9]]]]
I hope that's clear. Good luck!
list1 = [1,2,3,4,5,6,7,8,9]
list2 = [[[[1,2,3,4,5,6,7,8,9]]]]
list1 is a list.
list2 is a list of list of list of list.
len(list1) #will get you 9.
len(list2) #will get you 1.
You will either have to iterate into the final list inside list2 or somehow flatten it into a one dimensional list.
Hope this helps point you in the right direction without directly giving out the answer.
I have this snippet of code:
list1 = [1, 2, 3]
list2 = list1
list1 = [4, 5, 6]
print(list2)
print(list1)
Which results in the following output:
[1, 2, 3]
[4, 5, 6]
Why is list2 not still pointed to [4, 5, 6]? I was under the impression that, since lists are mutable, the change would affect both list1 and list2, since in RAM both lists are pointed to the same sequence of items.
Any explanation would be appreciated.
I have added the reason as comments in the code :
# list1 variable points to the memory location containing [1, 2, 3]
list1 = [1, 2, 3]
# list2 variable made to point to the memory location pointed to by list1
list2 = list1
# list1 variable made to point to the memory location containing
# a new list [4, 5, 6], list2 still pointing to [1, 2, 3]
list1 = [4, 5, 6]
print(list2) # list2 prints [1, 2, 3]
print(list1) # list1 prints [4, 5, 6]
I'll go through the lines one by one:
# Define a list [1, 2, 3] and save it into list1 variable
list1 = [1, 2, 3]
# Define list2 to be equal to list1 (that is, list2 == list1 == [1, 2, 3])
list2 = list1
# Create a new list [4, 5, 6] and save it into list1 variable
# Notice, that this replaces the existing list1!!
list1 = [4, 5, 6]
# Print list2, which still points to the original list [1, 2, 3]
print(list2)
# Print the new list1, [4, 5, 6] that is
print(list1)
However, this:
list1 = [1, 2, 3]
list2 = list1
list1.append(4)
print(list2)
print(list1)
Will output:
[1, 2, 3, 4]
[1, 2, 3, 4]
Since we are editing the list1 (and therefore list2, they are mutable), not creating a new list and saving it under the variable name list1
The keyword here is create a new list, so you're not editing list1 in your example, you're actually changing the name list1 to point to a whole different list.
Lists are mutable. However, the line:
list1 = [4, 5, 6]
does not mutate the list object previously referenced by list1, it creates a brand new list object, and switches the list1 identifier to reference the new one. You can see this by looking at object IDs:
>>> list1 = [1, 2, 3]
>>> list2 = list1
>>> id(list1)
4379272472
>>> id(list2)
4379272472 # both reference same object
>>> list1 = [4, 5, 6]
>>> id(list1)
4379279016 # list1 now references new object
>>> id(list2)
4379272472 # list2 still references previous object
im trying to write a fun script for a password generator that DOES NOT have any rational connection to the user, thus making it alot harder to use the fact you know the user in order to guess it.. but im new to python
basically i know how to use append and extend but i was trying to find a way to add a whole list to another, for example is list1 is (1 2 3 4) and list2 is (3 4 5 9) i want to know how to addlist2tolist1` and get a list1: (1 2 3 4 3 4 5 9)
ty!
This can be done very simply using the extend method. The following is a quick demonstration:
>>> l1 = [1,2,3]
>>> l2 = [3,4,5]
>>> l1.extend(l2)
>>> l1
[1, 2, 3, 3, 4, 5]
The addition operator has the same effect:
>>> l1 + l2
[1, 2, 3, 3, 4, 5]
Just use the concatenate overloaded operator:
list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 9]
list1 + list2
=> [1, 2, 3, 4, 3, 4, 5, 9]
The above will create a new list with the result of concatenating list1 and list2. You can assign it to a new variable if necessary.
You have 2 ways (as far as I know). Let's:
list1 = [1,2,3]
list2 = [4,5,6]
First:
list1 = list1 + list2
print list3
>>> [1,2,3,4,5,6]
Second:
list1.extend(list2)
print list1
>>> [1,2,3,4,5,6]
Also remember that (1,2,3) is a tuple not a list.
Addition concatenates lists:
list1 + list2
I have 2 arrays:
arr1 = [a,b,c,d,e]
arr2 = [c,d,e]
I want to give array arr1 except arr2.
Mathematically, you're looking for a difference between two sets represented in lists. So how about using the Python set, which has a builtin difference operation (overloaded on the - operator)?
>>>
>>> arr = [1, 2, 3, 4, 5]
>>> arr2 = [3, 4, 9]
>>> set(arr) - set(arr2)
>>> sdiff = set(arr) - set(arr2)
>>> sdiff
set([1, 2, 5])
>>> list(sdiff)
[1, 2, 5]
>>>
It would be more convenient to have your information in a set in the first place, though. This operation suggests that a set better fits your application semantics than a list. On the other hand, if you may have duplicates in the lists, then set is not a good solution.
So you want the difference of two lists:
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list2 = [1, 2, 3, 4, 4, 6, 7, 8, 11, 77]
def list_difference(list1, list2):
"""uses list1 as the reference, returns list of items not in list2"""
diff_list = []
for item in list1:
if not item in list2:
diff_list.append(item)
return diff_list
print list_difference(list1, list2) # [5, 9, 10]
Or using list comprehension:
# simpler using list comprehension
diff_list = [item for item in list1 if item not in list2]
print diff_list # [5, 9, 10]
If you care about (1) preserving the order in which the items appear and (2) efficiency in the case where your lists are large, you probably want a hybrid of the two solutions already proposed.
list2_items = set(list2)
[x for x in list1 if x not in list2_items]
(Converting both to sets will lose the ordering. Using if x not in list2 in your list comprehension will give you in effect an iteration over both lists, which will be inefficient if list2 is large.)
If you know that list2 is not very long and don't need to save every possible microsecond, you should probably go with the simple list comprehension proposed by Flavius: it's short, simple and says exactly what you mean.