Python checking if string in list to list - python

I am having a hard time trying to understand how the "for" function works.
I want to make a script that only outputs the strings in list2 that are not inside list1. For example:
list1 = ["link1.com", "link2.com", "link3.com"]
list2 = ["link2.com", "link123.com"]
for list2 in list1:
print(list2)
{My intention was that the code printed:
link123.com
But instead it printed the strings from list1}
I can't get it to work. Help would be much appreciated. I am using python 3 by the way.

Use Set for this .
set(list2)-set(list1)
Check with python set

The loop for list2 in list1 is actually an assignment: in each iteration the variable list2 gets the value of the next item in list1 - that is why only the elements of list1 are printed.
You could iterate over the elements of list2 and print, if they are not in list1:
for element in list2:
if element not in list1:
print(element)

Or if you want to use a for loop (note that this isn't very efficient for large lists):
for string in list2:
if not string in list1:
print (string)

For loops
For loops allow you to repeat a piece of code multiple times. You could easily just write a conditional and a print statement multiple times for each item in the list. A for loops allows you to write that piece of code once and have it repeated for each item in the list.
You can iterate over item in list2 by doing the following
for item in list2:
print(item)
item is an arbitrary variable name that holds the value of the current item we are on, what follows in is the list that we want to iterate over. print(item) is the piece of code we want to repeat for each element in the list.
What this does is goes through every item in list2 and prints them but that is not what we want. We want to check to make sure that item is not in list1. This can be achieved through a conditional statement.
if item not in list1:
print(item)
Now we can join the two piece of code together.
for item in list2:
if item not in list1:
print(item)
Sets
Are a collection of items in no order where every item is unique. These sets are the same ones we encounter in mathematics, therefore we can perform mathematical set operations on them.
To go from a list of items to a set we use sList1 = set(list1) sList1 is now of type set and stores the elements in list1. The same can be done for list2.
Now that we have sList1 and sList2 we want to eliminate any duplicates in the two for that we can take the difference of sList1 and sList2 and print them out as follows print(sList2-sList1).
We can do all of this in one step.
print( set(list2) - set(list1) )

The semantic, that python will check if these items are in list1 is NOT part of the for-each-loop.
The 'set' - solution is maybe too advanced for you.
So straightforward you would:
for item in list2:
if item not in list1:
print(item)

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})))

Find the nearest value of a list element

I have two lists which are:
>>> list1 = ['gain','archive','win','success']
>>> list2 = ['i','win','game','i','am','success','cool']
and also I found the same values of both list by comparing the lists.
>>> result= set(list1) & set(list2)
Output is
set(['win', 'success'])
Now I want to find the next element value of the result. Here it would be: 'game' and 'cool'.
How can I do this (using python 2.7)?
Given that you have the intersection words
result = { 'win', 'success' }
You could find the next words in list2 like this:
next_words = [list2[list2.index(word)+1] for word in result]
index gets you the index of the given element in the list. You can add 1 to it to get the next element.
If your element is at the end of the list, it will throw an exception, because there is no "next" element to get.
You can use the index function and add 1. Be careful though, if your common element is the last one of your list, it will generate an error
list1 = ['gain','archive','win','success']
list2 = ['i','win','game','i','am','success','cool']
result= set(list1) & set(list2)
list3 = [list2[list2.index(e)+1] for e in result]
edit For the case where you last element is a common element:
result= set(list1) & set(list2)
list4 = []
for e in result:
try:
list4.append(list2[list2.index(e)+1])
except:
pass
Output: ['game', 'cool']
You could do a pairwise iteration over your list2 and do the "intersection" manually:
list1 = ['gain','archive','win','success']
list2 = ['i','win','game','i','am','success','cool']
set1 = set(list1)
result = []
for item, nextitem in zip(list2, list2[1:]): # pairwise iteration
if item in set1:
result.append(nextitem) # append the next item if the current item is in the intersection
print(result) # ['game', 'cool']
This does the trick for the next element in list2:
next_result = [list2[list2.index(el)+1] for el in result if list2.index(el)+1<len(list2)]
You could use list2.index, but that's doing a full search just for finding back an index, and artificially increasing complexity from O(n) to O(n*n).
Just keep track of the indexes of each words. There are several ways to do that.
Create your own function that search for common words, and return them as the index of those words in list2. This probably the least pythonic but the fastest.
Create a dictionary from the words of list2 to their index, then after computing the set intersection, lookup on the dict to find the index and increase by one. You need to build a full dictionary the size of list2, this might be expensive (but still better than O(n*n)).
Create a dictionary from the words of list2 to their next word or None if there aren't and do a lookup on the dict to find the index. You need to build a full dictionary the size of list2, this might be expensive.
If you know how to use itertools, you could do an iterator on list2 that yield the index and the word, and filter the result if the word is in list1, then pick only the indexes.

Iterating through a 2D list using another list

The code below seems to be iterating through a 2d list using another list which conceptually doesn't make much sense to me.
What would be the in range equivalent to the code below, using lens as I'm finding it quite difficult to understand.
I've changed the variable names as I'm working on coursework but if its too abstract I can add in the original variable names.
#list2 is a 2d list
#list1 is a normal list
for list1 in list2
for k in range(n) #n and k are constants
#any if statement
A "2D" list is just a list where each element is itself a list. To access each element of the lists inside the "main" list, do
for list1 in list2:
for element in list1:
print(element)
If you want a version using range:
L2 = len(list2)
for i in range(L2):
list1 = list2[i]
L1 = len(list1)
for j in range(L1):
element = list1[j]
print(element)
As should be clear from the above, using range in a for loop is very rarely a good idea, as the code is much less readable.

while loop - IndexError: list index out of range

Maybe it's too simple and I just didn't see my mistake.
while list_a[y] in list_a != list_a[-1]:
print(y);y=y+1
returns IndexError: list index out of range
list_a looks like:
['00001', '00001', '00002', '00009', '0000G', '0000K', '0000K', '0000U', '0000U', '00013', '0001B', '0001D', '0001D', '0001L', '0001L', '0001N', '0001Q', '0001Q', '0001R', '0001U']
and my aim in the end is to delete some items from the list while iterating (that's why I want to use a while loop instead of for y in range(len(list_a))).
Think what you were trying for was:
while list_a[y] != list_a[-1]:
...
i.e. "while we're looking at the item that isn't equal to the last in the list". However, there will still be issues; what if items appear elsewhere in the list that are equal to the last item?
The general way to do this is to use a list comprehension to build a new list from the appropriate items in the old list:
list_b = [item for item in list_a if some_test(item)]

Python-for statements understanding

I am a little confused as to what a for statement does/works in python. Can anyone be able to explain to me on how it works?
For loops allow you to express a repetitive action.
For example, if we want to print a list we could do the following:
mylist = ['apples', 'oranges', 'pears']
item = mylist[0]
print item
item = mylist[1]
print item
item = mylist[2]
print item
This can be expressed more concisely, as follows:
mylist = ['apples', 'oranges', 'pears']
for item in mylist:
print item
The loop will repeat as long as there are items left in the sequence.
There are 3 items in mylist, so the loop will repeat 3 times.
On each iteration of the loop (every time it repeats), the variable 'item' will be given the next value in the sequence.
That is:
1st iteration: item -> 'apples'
2nd iteration: item -> 'oranges'
3rd iteration: item -> 'pears'
If you have a sequence, e.g. a list:
l = [1,2,3]
you can iterate over the list via:
for i in l:
and i will always be one element of the list.
Find here information about sequences.
There are functions that generate lists, e.g range. So
for($i=0;i<10;$i++)
in PHP translates to
for i in xrange(10):
in Python.
for in Python is similar to PHP's foreach or Java's enhanced for (... : ...) loop.
http://docs.python.org/tutorial/controlflow.html#for-statements
'for' loop in python, is a mature way of going through a list.
As so many clever people has said: Human languages are fuzzy, code is not. Programmers should communicate in code:
This is what a for statement does:
>>> for item in ['this', 'is', 'a', ('list', 'of'), 6, 'elements']:
... print "The item is:", item
The item is: this
The item is: is
The item is: a
The item is: ('list', 'of')
The item is: 6
The item is: elements
Clearer now?
Well, the for statement in Python iterates over a sequence which may be a list or a string. You always loop over items as they appear in the sequence. This differs from languages like PHP and C# where you can control this for(x = 0; x < y; x++).
Do you want to know something more? If so, please elaborate.

Categories