473,396 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to get the empty list?

440 256MB
Hi,

I would like to clear /delete all the list data.How to procees without assigning empty data.Is there any method available?.

Say:

list1 = [1,2,3]

I dont want to do list1 =[]

-PSB
Apr 19 '07 #1
8 2169
psbasha
440 256MB
Hi ,

Below is the sample code,where I am expecting the list should return empty [],but it is returning the [1,2].

Whether " listData = []" is taking in function Func1() as local variable because of the declaration.

How to overcome this issue?

Expand|Select|Wrap|Line Numbers
  1. Sample
  2.  
  3. def Func1(Flag,listData):
  4.     if Flag:
  5.         listData = []
  6.     else:
  7.         listData.append(1)
  8.         listData.append(2)
  9.  
  10.  
  11. if __name__ == '__main__':
  12.  
  13.     listData = []
  14.     Func1(True,listData)
  15.  
  16.     Func1(False,listData)
  17.  
  18.     Func1(True,listData)
  19.  
  20.     print listData 
  21.  
  22. >>> [1, 2]
  23.  
  24.  
-PSB
Apr 19 '07 #2
ilikepython
844 Expert 512MB
Hi ,

Below is the sample code,where I am expecting the list should return empty [],but it is returning the [1,2].

Whether " listData = []" is taking in function Func1() as local variable because of the declaration.

How to overcome this issue?

Expand|Select|Wrap|Line Numbers
  1. Sample
  2.  
  3. def Func1(Flag,listData):
  4.     if Flag:
  5.         listData = []
  6.     else:
  7.         listData.append(1)
  8.         listData.append(2)
  9.  
  10.  
  11. if __name__ == '__main__':
  12.  
  13.     listData = []
  14.     Func1(True,listData)
  15.  
  16.     Func1(False,listData)
  17.  
  18.     Func1(True,listData)
  19.  
  20.     print listData 
  21.  
  22. >>> [1, 2]
  23.  
  24.  
-PSB
Well you can have the function return a value. LIke this:
Expand|Select|Wrap|Line Numbers
  1. def func1(Flag, listData):        //can also work w/o second arguement
  2.     if Flag:                             
  3.         listData = []
  4.         return listData
  5.     else:
  6.         listData.append(1)
  7.         listData.append(2)
  8.         return listData
  9.  
Not sure if that's the best way though.
Apr 19 '07 #3
psbasha
440 256MB
This is a sample piece of code.But I have some more statements to be executed.I cannot return the values.

-PSB
Apr 19 '07 #4
psbasha
440 256MB
But in this case call by reference approach is broken.How to handle this situation?

-PSB
Apr 19 '07 #5
bartonc
6,596 Expert 4TB
This is a sample piece of code.But I have some more statements to be executed.I cannot return the values.

-PSB
>>> aList = [1, 2, 3]
>>> del aList[:]
>>> aList
[]
>>>
Apr 19 '07 #6
bartonc
6,596 Expert 4TB
Hi,

I would like to clear /delete all the list data.How to procees without assigning empty data.Is there any method available?.

Say:

list1 = [1,2,3]

I dont want to do list1 =[]

-PSB
see below -- try not to double post you questions.

Thanks.
Apr 19 '07 #7
psbasha
440 256MB
Hi Barton,

Thanks for the solution.It really helped me.I was trying to do the code modularity,where for some reason in my code ,I need to assign the empty list , .

I almost spend more time by trying to get the solution,finally I thought I will post in the forum.

Thanks once again Barton

PSB
Apr 19 '07 #8
bartonc
6,596 Expert 4TB
Hi Barton,

Thanks for the solution.It really helped me.I was trying to do the code modularity,where for some reason in my code ,I need to assign the empty list , .

I almost spend more time by trying to get the solution,finally I thought I will post in the forum.

Thanks once again Barton

PSB
That's what we're here for. You are welcome.
Apr 19 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I...
3
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns...
3
by: Dave Opstad | last post by:
I fully understand why this happens: ---------------------------- >>> a = , , , , ] >>> b = ] * 5 >>> a , , , , ] >>> b , , , , ] >>> a == b
10
by: Brian Roberts | last post by:
I'm using using generators and iterators more and more intead of passing lists around, and prefer them. However, I'm not clear on the best way to detect an empty generator (one that will return no...
25
by: Matthias | last post by:
Hi, I am just reading that book by Scott Meyers. In Item 4 Meyers suggests to always use empty() instead of size() when probing for emptyness of STL containers. His reasoning is that size()...
59
by: Steve R. Hastings | last post by:
So, Python 2.5 will have new any() and all() functions. http://www.python.org/dev/peps/pep-0356/ any(seq) returns True if any value in seq evaluates true, False otherwise. all(seq) returns...
20
by: Sun | last post by:
Maybe this is a very primative question, but I just get a bit confused about 'set' and 'Set' module in python. I understand 'set' is a build in type in python after 2.4(or 2.3) and Set a...
1
by: Tzury Bar Yochay | last post by:
while I can invoke methods of empty string '' right in typing (''.join(), etc.) I can't do the same with empty list example: I would not use b = a since I don't want changes on 'b' to...
2
by: Matthew Fitzgibbons | last post by:
Alexnb wrote: if not funList: do_something() -Matt
0
by: Hans Kesting | last post by:
Hi, I want to edit an entire list on a single page, say a shopping list where every 'item' consists of a name and an amount. I don't know beforehand how many items will be needed. Using a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.