0
votes

I have a list with 500 repeating names:

[name1, name2, name1, name7,name2,name1, name1, name1, name134, name11, etc...]

I want to create a list with only the base names for example if there are only N base names then the list should be:

[name1, name2, name3, name4, ... , name(N)]

how would I do this the fast and easy way?

3
Are you saying you want to get rid of repeating names? Assuming your list is stored in names, you can do set(names) to get unique names. - Chrispresso
Could you provide code that illustrates how you tried to solve this problem - that way it is easier for people to provide useful and timely answers. - Eric Broda

3 Answers

1
votes

What do you mean by base names? To get a list of unique names, you can just do:

list(set(names))

This converts the initial list to a set, and you're again converting the set to a list

0
votes

With set() you can have an unordered collections of unique elements:

myList = ['name1', 'name2', 'name1', 'name7', 'name2', 'name1', 'name1', 'name1', 'name134', 'name11']
mySet = set(myList)
print(mySet)

{'name134', 'name11', 'name2', 'name1', 'name7'}

The list() constructor returns a mutable sequence list of elements:

myNewList = list(mySet)
print(myNewList)

['name134', 'name11', 'name2', 'name1', 'name7']

sorted() returns a new sorted list:

myNewSortedList = sorted(myNewList)
print(myNewSortedList)

['name1', 'name11', 'name134', 'name2', 'name7']

0
votes
list_of_repeating_names = [name1, name2, name1, name7,name2,name1, name1, name1, name134, name11, ]
sorted_unique_names = sorted(list(set(list_of_repeating_names)))

returns

['name1', 'name11', 'name134', 'name2', 'name7']

The set deletes the duplicates but is unordered, you convert it into a list that you sort.