0

I'm trying to apply a set() function to multiple objects of lists. Below is the example of said lists:

test_lst1 = ['a','b','c','d','e','b','e']
test_lst2 = ['z','x','y','z']

I want to do something along the line of:

for x in range(2):
    test_lst{x} = set(test_lst{x})

Any idea how I can do that?

Edit

I decided to transform the data frame into a dictionary and assign each column while taking the duplicates through a for-loop.

test_dict = {}
for col in test_df :
    test_dict[col] = test_df[col].unique()
  • 1
    Variable name is just a name which used by developer to work with data, you shouldn't build names dynamically even if it's possible. Define a [dictionary](https://docs.python.org/3/tutorial/datastructures.html#dictionaries) which is key-value data storage if you want to access data by key. – Olvin Roght Jul 21 '22 at 14:08
  • This feels like an [XY-problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You are probably trying to do this to solve some other problem. It would be better to ask about that real problem you're trying to solve instead of about what you think is the solution for it – Tomerikoo Jul 21 '22 at 14:14
  • For example, what is the source of these lists? maybe they could be made sets somewhere earlier in the code. Why are there multiple separate variables of lists instead of a list of lists or a dict? That would simplify the handling of such structure – Tomerikoo Jul 21 '22 at 14:16
  • Hi @Tomerikoo , thanks for the feedback! So, I have multiple csv files where each file has 17 columns. Basically, I have concatenated these csv files into one data frame. Now, I need to convert each column into a list that only contains unique values. The purpose of having multiple lists is to match them with tokenized words from multiple URLs. I hope that clarifies my original question. – mangarapaul Jul 21 '22 at 14:31
  • 1
    Yes, as I suspected, there is an easier way to solve your problem from the root and not needing to even get to what you are asking about. It is already asked and answer here - [print the unique values in every column in a pandas dataframe](https://stackoverflow.com/q/27241253/6045800) – Tomerikoo Jul 21 '22 at 14:33
  • Thanks for the directions @Tomerikoo ! I decided to transform the data frame into a dictionary and remove the duplicates while assigning it to the dict. – mangarapaul Jul 21 '22 at 15:00

1 Answers1

1

If you are trying to apply the set function to both test_lst1 and test_lst2 using a for loop, you would do:

for item in [test_lst1, test_lst2]:
    item = set(item)
    print(item)
statnet22
  • 444
  • 2
  • 13