Get index of the deleted elements while using symmetric difference

set symmetric difference python
symmetric difference example
symmetric difference set theory proof
symmetric difference associative
set operations
python set
get element in set python
symmetric difference calculator

There are two columns in a CSV file:

oldCol1 = [1, 2, 3, 4, 5]

oldCol2 = ['A', 'B', 'C', 'D', 'E']

Now I update the csv and add a new row

newCol1 = [1, 2, 3, 4, 5, 6]

newCol2 = ['A', 'B', 'C', 'D', 'E', 'A']

I want to get only the newly added element. So, I am trying this:

newListCol1 = list(set(oldCol1).symmetric_difference(newCol1))

Now, my question is how to get the newly added element from column two?

#Here, I want to get two lists: [6] and ['A']. 

Thanks for your help!

Update: The newly added elements can be anywhere in the list (not just the end) -- sorry for the confusion!

#if they can be anywhere

#mehtod 1
from collections import Counter 

oldCol1 = [1, 2, 3, 4, 5]

oldCol2 = ['A', 'B', 'C', 'D', 'E']

newCol1 = [1, 2, 3, 4, 5, 6]

newCol1_1 = [1, 2, 3, 4, 5, 6, 6, 7, 7] #different example

newCol2 = ['A', 'B', 'C', 'D', 'E', 'A']

print(list((Counter(newCol1) - Counter(oldCol1)))) # returns a list of unique value
print(list((Counter(newCol2) - Counter(oldCol2))))


new_item_added_dict = Counter(newCol1_1) - Counter(oldCol1)
print( list(new_item_added_dict.elements())) # elements() returns an iterator
# if you want all the new values even duplicates like in newCol1_1 
# ie if you want ans to be [6, 6, 7, 7] then use elements()

# else use list() if you just want unique value updates [6,7]
print( list(new_item_added_dict))

 # output
 # [6]
 # ['A']
 # [6, 6, 7, 7]
 # [6, 7]

#--------------------------------------------------------------------- 

#method 2
from collections import defaultdict
oldCol1 = [1, 2, 3, 4, 5]
newCol1 = [1, 2, 3, 4, 5, 6]  # -->[6]
# [1, 2, 3, 4, 5, 6, 5] --> [6,5]

new_item_list = []
oldlist_dict = defaultdict(lambda:0) #default value of key is 0 and with defualtdict you will not key error

for item in oldCol1:
    oldlist_dict[item] += 1

for item in newCol1:
    if item in oldlist_dict and oldlist_dict[item] > 0:
        oldlist_dict[item] -=1
    else:
        # its a new item 
        new_item_list.append(item)

print(new_item_list)


#--------------------------------------------------------------------- 

#if new items are always appended ie added to end of old list
print(newCol1[len(oldCol1):])  

print(newCol2[len(oldCol2):])

print(newCol1_1[len(oldCol1):])  

Symmetric Difference, Hello guys, i'm trying to finish the Symmetric Difference challenge but i can't seem to go but i did not think of using a third array to store the elements. 2, 3]], I'd like to delete the repeated 3s and 6s and get the following array back You probably want indexOf a value in an array not of another index like j. 1 Get index of the deleted elements while using symmetric difference Aug 14 '18 1 I want to print the “\” character in python but I get “\\” Aug 21 '18 View all questions and answers →

If you know that the "newly added elements" are always appended to the end of the list, then just slice from the length of the old list onward. i.e.

old_til_here = len(oldCol1)
list_of_new_elements_col_1 = newCol1[old_til_here:]
list_of_new_elements_col_2 = newCol2[old_til_here:]

Sets in Python, Since set items are not indexed, sets don't support any slicing or indexing it is possible that you will get an output with the elements arranged in a different order​. The set has removed the duplicates and returned only one of each duplicate items. We can also check for the presence of an element in a set using the in  Form the intersection of two Index objects. Index.union (self, other[, sort]) Form the union of two Index objects. Index.difference (self, other[, sort]) Return a new Index with elements from the index that are not in other. Index.symmetric_difference (self, other[, …]) Compute the symmetric difference of two Index objects.

You will need to get indexes that are not present in first one, so use just sets without symmetric_difference. Use enumerate() to get indexes easier.

oldCol1 = [1, 2, 3, 4, 5]

oldCol2 = ['A', 'B', 'C', 'D', 'E']

newCol1 = [1, 2, 3, 4, 5, 6]

newCol2 = ['A', 'B', 'C', 'D', 'E', 'A']

indexes = [i for i, v in enumerate(newCol1) if v not in set(oldCol1)]

resultCol1 = [newCol1[i] for i in indexes]
resultCol2 = [newCol2[i] for i in indexes]

print(resultCol1, resultCol2)

Symmetric difference, In mathematics, the symmetric difference, also known as the disjunctive union, of two sets is the The power set of any set becomes a Boolean ring with symmetric difference as the addition of In the case where X has only two elements, the group thus obtained is the Klein four-group. is an arbitrary non-​empty index set. This method is used to return the symmetric difference of a set and the set of elements from the iterable like string, list, set passed as an argument. This method takes one iterable object as argument, if iterable other than set is passed, it first converts the iterable object to set object and then perform the symmetric difference operation. It

pandas.Index.symmetric_difference, Home · What's New in 1.0.1 · Getting started · User Guide · API reference Index.​delete · pandas. Compute the symmetric difference of two Index objects. symmetric_difference contains elements that appear in either idx1 or idx2 but not both. Index created by idx1.difference(idx2) | idx2.difference(idx1) with duplicates  symmetric_difference_update method is used to find the symmetric difference of two sets in python. For two sets setA and setB, the symmetric difference is the set of all elements that are either in setA or setB but not in both. For example : setA = {1,2,3,4} setB = {3,4,5,6} The symmetric difference between above two sets is {1,2,5,6}.

Python Sets, You cannot access items in a set by referring to an index, since sets are The return value of the pop() method is the removed item. Note: Sets are unordered​, so when using the pop() method, you will not know which item that gets removed symmetric_difference(), Returns a set with the symmetric differences of two sets. Computes symmetric difference of two sorted ranges: the elements that are found in either of the ranges, but not in both of them are copied to the range beginning at d_first. The resulting range is also sorted. If some element is found m times in [first1, last1) and n times in [first2, last2), it will be copied to d_first exactly std:: abs (m-n) times

Sets in Julia, Getting type of keys and values of a Dictionary in Julia - keytype() and Sets are mutable data types, which means its values can be altered, deleted, overwritten, etc. Julia allows adding new elements in a set with the use of ' push! the only difference is, unlike arrays, elements in set can't be added on a specific index  This code will return the symmetric difference of two sets and will take both cell arrays and vectors (as in the above example) as inputs. function resultantSet = symmetricDifference ( set1,set2 ) assert ( ~xor ( iscell ( set1 ) ,iscell ( set2 ) ) , 'Both sets must be of the same type, either cells or matricies, but not a combination of the two' ) ;

Comments
  • Ill defined problem. What's the definition of "newly added element"? If newcol1 = [1,1,2,3,4,5] did you add the first or second 1?
  • If a new row is added, for example (6, 'A') -- there are two columns, then how would I get that? newListCol1 = list(set(oldCol1).symmetric_difference(newCol1)) gives me [6] -- that is correct. I don't know how to get ['A']. I hope that helps.
  • @Julien...what? I added a 6 in the end.
  • If you added 6 and A then the answer is 6 and A. If you want a program for the general case, you need to specify correctly the general case.
  • The general case would be that any number of rows can be added. But that's not the point. How would you programmatically get ['A'] here? -- that's my question.
  • Glad I could help :) read about Counter from docs docs.python.org/3/library/collections.html#collections.Counter
  • Great idea but I can not guarantee that condition. Thanks for your help though!
  • Sets don't work well when you have to know the exact count of repeated elements.
  • I assume that column names are unique repeat because in case they do we are unable to distinguish if the columns were swapped and many problems arise :)
  • I posted a clarification. Sorry about the confusion. @MadPhysicist