First time here? We are a friendly community of Power Systems Engineers. Check out the FAQ!

Ask Your Question
1

How to check the existence of an element in an array ?

asked Apr 30 '13

Mongo gravatar image

Hi friends,

Please, suppose that I have:

fr = [ 2,5,6]

Now I wanna check if a 4th element exists in the array, and the result would be NO, please is there a way to check it ?

Please note that I wanna check if an element exists in a certain position, not the element itself;

Thanks,

Andre

1 answer

Sort by » oldest newest most voted
1

answered May 1 '13

Eli Pack gravatar image

To check if a 4th element exists, you just need to determine the size of the list. The following line will check to see if a 4th element exists:

len(fr) >= 4

Note that even if you delete an item from the middle of your list, the indexes of the items after it will be shifted so that there are no gaps in your list.

link

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.
Want to format code in your answer? Here is a one minute demo on Youtube

Add Answer

[hide preview]

Question Tools

Stats

Asked: Apr 30 '13

Seen: 269 times

Last updated: May 01 '13