473,516 Members | 2,865 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to check if the element is contained in list or not?

8 New Member
Hello,

I have some problem with checking if the element is contained in the list or not.

For example:
a = ['abc', 'def', 'gh', 'i']
b = 'abc'

Following is my code:
Expand|Select|Wrap|Line Numbers
  1. def is_proper_word(b, a) :
  2.  
  3.     is_proper = False
  4.     for b in proper_word_list:
  5.         is_proper = True
  6.     return is_proper
  7.  
Is it correct?

Thank you in advance.
May 2 '10 #1
4 2184
Glenton
391 Recognized Expert Contributor
Hi

With a and b as you suggest
Expand|Select|Wrap|Line Numbers
  1. b in a
returns True.

It seems unnecessary to do more work than this single line, but if you have a reason post back.

Actually post back anyway to let us know how it goes.
May 2 '10 #2
Glenton
391 Recognized Expert Contributor
By the way, your code would not work. It would simply create a loop of all the variables in proper_word_list, (which isn't defined anyway), calling the items in the list variable b. It would just over-write the b variable that you input.
May 2 '10 #3
Nonemsludo
15 New Member
@Jimmy45
You could use list comprehension,
[elem for elem in a if elem == b]
May 2 '10 #4
Glenton
391 Recognized Expert Contributor
Oh, if you're wanting to do it that way, you could use numpy arrays with a in b giving a boolean numpy array. Depends what you want to do really.
May 3 '10 #5

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

Similar topics

1
1556
by: Holger Butschek | last post by:
Hy folks, I have a problem in Zope (DTML). I know, that it's a very simple problem, but I'm quite new to Zope. I want to check, if a sequence-item of a sql-statement is in a list called groupsSW (this list might or might not be given by URL from a previous window).
20
2248
by: xerix | last post by:
I'm trying to make a function which insert an object into a list and returns an iterator to the new element in the list. But as far as I know there is no function which returns an iterator to the last element of a list. (.end() returns an iterator one item past the end of the list) How could I solve this?
3
7645
by: arvind.nm | last post by:
hi, if i create a linked list like list<char> stk; if i want to remove the first element from the list and i write stk.pop_front() the node is still present but the char stored is now '\0',why? how can i delete the first node. will stk.erase(stk.begin()) work?
3
1720
by: Vijay | last post by:
Hi, Iam new to VC++6.0. I want to display selected item of the ListBox in a MessageBox. I know for experience ppl it must be a kids play but for me who had worked in C# and VB, it seems to be little different. Can anyone write couple to lines of code for this? Moreover, If I want to insert any element at the mth place of nth col in list box...
12
9430
by: Howard | last post by:
Is there an easy way to get an iterator (*not* a reverse-iterator) to the last element in a list? The last() function returns the element itself, not an iterator. Thanks, -Howard
0
7182
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7408
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7581
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7548
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4773
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3267
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3259
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1624
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
488
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.