Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

rename duplicates in list python

[v + str(mylist[:i].count(v) + 1) if mylist.count(v) > 1 else v for i, v in enumerate(mylist)]
Comment

python rename duplicate strings in list

mylist = ["name", "state", "name", "city", "city", "name", "zip", "zip", "name"]

dups = {}

for i, val in enumerate(mylist):
    if val not in dups:
        # Store index of first occurrence and occurrence value
        dups[val] = [i, 1]
    else:
        # Special case for first occurrence
        if dups[val][1] == 1:
            mylist[dups[val][0]] += str(dups[val][1])

        # Increment occurrence value, index value doesn't matter anymore
        dups[val][1] += 1

        # Use stored occurrence value
        mylist[i] += str(dups[val][1])

print mylist

# ['name1', 'state', 'name2', 'city1', 'city2', 'name3', 'zip1', 'zip2', 'name4']
Comment

PREVIOUS NEXT
Code Example
Python :: python paragraph Pypi 
Python :: how to import a all the modules in a packege python 
Python :: k-means clustering and disabling clusters 
Python :: mechanize python #11 
Python :: width and precision thousand separetor python 
Python :: pseudo-random input signal python 
Python :: sort dictionary by values 
Python :: snake game using python 
Python :: python int rightpad with 0 
Python :: get_multiple_items_from_list 
Python :: python urllib.request.urlretrieve with a progressbar 
Python :: how to click the next button on a website using python 
Python :: # colab, display the DataFrame in table format 
Python :: Find number of triangles that can be made by given sides of triangle 
Python :: matplotlib get colorwheel 
Python :: pairplot lower triangular 
Python :: Insertion Sorting using while in python 
Python :: Python getting content from xl range 
Python :: Find Factors of a Number using While Loop with validation 
Python :: List Comprehension simple example 
Python :: maximum of a list in python recursively 
Python :: how to get mid time of given time in python 
Python :: python create empty list with size 10 
Python :: build numpy array 
Python :: Broadcasting with NumPy Arrays Plotting a two-dimensional function Example 
Python :: get nodes of xml in python 
Python :: Python NumPy asarray_chkfinite Function Example List to an array 
Python :: configure socketio static file python specific content type 
Python :: Python __le__ 
Python :: calculate mse loss python 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =