I have a list, would like to know how to hide the elements with a *
thanks
henry
I have a list, would like to know how to hide the elements with a *
thanks
henry
What do you mean by “hide the elements”?
Hello,
I don’t know how flexible you are, but you can potentially use a class
to both encapsulate and “hide
” the original list by creating a copy but replacing the list elements
with asterisks *
. Note that the example that I have provided is very basic in nature. You can of course use it along with an encryption module
to really enrypt the actual elements in the list (self.__data_list
). The idea is that you have two versions: One for the world to see and the other that is private and only accesible by you via a method call.
# Assuming this is the data obtained in your script for 'hiding'
orig_data = ['today', 'tomorrow', 'yesterday', 1, 2, 3]
class Asterisk_Encrypt:
asterisk_list = [] # Hide data with '*'
def __init__(self, data):
self.__data_list = data # Keep original data
self.list_encrypt() # At instantiation, immediately hide data
def list_encrypt(self):
for add_data in self.__data_list:
if type(add_data) == str:
Asterisk_Encrypt.asterisk_list.append('*' * len(add_data))
else:
Asterisk_Encrypt.asterisk_list.append('*' * 5)
def get_data(self):
return self.__data_list
def del_index(self, index):
del(self.__data_list[index])
del(Asterisk_Encrypt.asterisk_list[index])
# Pass your list to class
My_List = Asterisk_Encrypt(orig_data)
# Fetch and print '*' encrypted list (what you want the world to see)
print('\n', My_List.asterisk_list, sep='')
# Fetch original data via class method (not directly via attribute qualification)
print(My_List.get_data())
# Delete index 2 element (as an example)
My_List.del_index(2)
# Fetch and print '*' encrypted list (what you want the world to see)
print('\n', My_List.asterisk_list, sep='')
# Fetch original data via class method (not directly via attribute qualification)
print(My_List.get_data())
When you run this script, you get:
# When you first instantiate the class with your original list
['*****', '********', '*********', '*****', '*****', '*****']
['today', 'tomorrow', 'yesterday', 1, 2, 3]
# After you have deleted an element (#2)
['*****', '********', '*****', '*****', '*****']
['today', 'tomorrow', 1, 2, 3]
To make it more robust, you can add an encryption module
to encrypt your list (self.__data_list
). There are encryption modules that you can quickly look up for immediate use.
One potential package (others are of course available):
Thank you to all who are helping me, to
Paul onePythonUser
April 12 |
- |
major effort from you, thanks, I am going to study your code to understand it better
jhenry
This might help. I do not know how to “hide” elements in a list. They are either in the list or they are not. This code removes elements in a list that begin with a ‘#’, ie. commented items.
I use this to ignore commented lines in data files.
mylist = ['one', '#two', 'three', 'five', '6six']
filtered_list = [item for item in mylist if not item.startswith('#')]
print(filtered_list)