#Hi friends I want to shorten these codes I must control at least 100 space and delete please help

for i in range(mylist.count('')):
		mylist.remove('')
	for i in range(mylist.count(' ')):  #delete 1 space
			mylist.remove(' ')
	for i in range(mylist.count('  ')):  #delete 2 space
			mylist.remove('  ')
	for i in range(mylist.count('   ')):  #delete 3 space
			mylist.remove('   ')

        ...........................


# help 2 I want to shorten below codes how to

if len(text1)<> 0:
	mylist.append(text1)
if len(text2) <> 0:
	mylist.append(text2)
if len(text3) <> 0:
	mylist.append(text3)
if len(text4) <> 0:
	mylist.append(text4)
if len(text5) <> 0:
    mylist.append(text5)
if len(text6) <> 0:
    mylist.append(text6)
if len(text7) <> 0:
    mylist.append(text7)
if len(text8) <> 0:
    mylist.append(text8)
if len(text9) <> 0:
    mylist.append(text9)
if len(text10) <> 0:
    mylist.append(text10)
if len(text11) <> 0:
    mylist.append(text11)
if len(text12) <> 0:
    mylist.append(text12)

I think that you want to loop through all the items in mylist, discarding any of them that are all spaces and keeping any that have length. You do not need to do it in place as your code tries to do but you do need to understand a little more "what it means" to loop through a collection of items.

The only tricky thing in the code below (and it is not all that much of a trick) is to know that an empty string acts as a boolean False.

You also need to know some non-tricky things: That string/unicode values have a method strip that removes leading and trailing white space, and that there is a built in function filter which returns only the elements of a list for which the filter function returns true. Understanding lambda syntax is optional as you can pass a "regular" function to filter if you wish.

I might do it like this (a way that is easy to understand but not the 'best' way):

def clean_list(mylist)
    """return a list holding only items that are non-nil and not all white space"""
    ret = []
    for item in mylist:
         if item.strip():
             ret.append(item)
    return ret

This way is less obvious to the beginner, but 'better' in a couple of ways:

  1. It uses the built in function filter which is probably more efficient
  2. It shows us the relevant code all in one place, which is easier to understand (once you understand lambda syntax and how filter works)
def clean_list(mylist):
    return filter(lambda x: str(x).strip(), mylist)

Griswolf's code assumes that you are looking for empty items in the list. If instead, you want to remove spaces within an item, i.e remove the space in "word1 word2", post back.

# help 2 I want to shorten below codes how to
##
## same as Griswolf's code: use a loop to replace code below 
test_list = [text1, text2, text3, text4, text5, text6, \
             text7, text8, text9, text10, text11, text12 ]
mylist = []
for item in test_list:
    if len(item):
        mylist.append(item)
#
#-------------------------------------------------------------------------
#  instead of
if len(text1)<> 0:
	mylist.append(text1)
if len(text2) <> 0:
	mylist.append(text2)
if len(text3) <> 0:
	mylist.append(text3)
if len(text4) <> 0:
	mylist.append(text4)
if len(text5) <> 0:
    mylist.append(text5)
if len(text6) <> 0:
    mylist.append(text6)
if len(text7) <> 0:
    mylist.append(text7)
if len(text8) <> 0:
    mylist.append(text8)
if len(text9) <> 0:
    mylist.append(text9)
if len(text10) <> 0:
    mylist.append(text10)
if len(text11) <> 0:
    mylist.append(text11)
if len(text12) <> 0:
    mylist.append(text12)

Here some more examples:

## same as Griswolf's and woooee's code: use a loop to replace code below
# use tuple, which is generally faster for looping and construction
# and automatic continuation inside parenthesis
#  BTW .remove removes all occurances of items
(text1, text2, text3, text4, text5, text6,
             text7, text8, text9, text10, text11, text12) = "  j  g 4 s f"

test_tuple = (text1, text2, text3, text4, text5, text6,
             text7, text8, text9, text10, text11, text12)
# use list comprehesion for not ' ' items like in OP first code
# note that you do not need lambda for list comprehension
mylist = [ item for item in test_tuple if item != ' ']
print mylist

# if items are single letters:
# make list from string
test_list = list("  j  g 4 s f")
# opposite and replace function which deals with removing all occurances by replace
# this solution is not so good as strings are immutable and it is better to use
# list comprehensin, generator or filter function.
test_string_without_blanks = ''.join(test_list).replace(' ','')
print test_string_without_blanks
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.