cbbcbail
cbbcbail

Reputation: 1771

Python Values in Lists

I am using Python 3.0 to write a program. In this program I deal a lot with lists which I haven't used very much in Python.

I am trying to write several if statements about these lists, and I would like to know how to look at just a specific value in the list. I also would like to be informed of how one would find the placement of a value in the list and input that in an if statement.

Here is some code to better explain that:

count = list.count(1)
if count > 1
    (This is where I would like to have it look at where the 1 is that the count is finding)

Thank You!

Upvotes: 0

Views: 158

Answers (4)

Andrew Clark
Andrew Clark

Reputation: 208665

Check out the documentation on sequence types and list methods.

To look at a specific element in the list you use its index:

>>> x = [4, 2, 1, 0, 1, 2]
>>> x[3]
0

To find the index of a specific value, use list.index():

>>> x.index(1)
2

Some more information about exactly what you are trying to do would be helpful, but it might be helpful to use a list comprehension to get the indices of all elements you are interested in, for example:

>>> [i for i, v in enumerate(x) if v == 1]
[2, 4]

You could then do something like this:

ones = [i for i, v in enumerate(your_list) if v == 1]
if len(ones) > 1:
    # each element in ones is an index in your_list where the value is 1

Also, naming a variable list is a bad idea because it conflicts with the built-in list type.

edit: In your example you use your_list.count(1) > 1, this will only be true if there are two or more occurrences of 1 in the list. If you just want to see if 1 is in the list you should use 1 in your_list instead of using list.count().

You can use list.index() to find elements in the list besides the first one, but you would need to take a slice of the list starting from one element after the previous match, for example:

your_list = [4, 2, 1, 0, 1, 2]
i = -1
while True:
    try:
        i = your_list[i+1:].index(1) + i + 1
        print("Found 1 at index", i)
    except ValueError:
        break

This should give the following output:

Found 1 at index 2
Found 1 at index 4

Upvotes: 4

Rob Wouters
Rob Wouters

Reputation: 16327

list.index(x)

Return the index in the list of the first item whose value is x. It is an error if there is no such item.

--

In the docs you can find some more useful functions on lists: http://docs.python.org/tutorial/datastructures.html#more-on-lists

--

Added suggestion after your comment: Perhaps this is more helpful:

for idx, value in enumerate(your_list):
    # `idx` will contain the index of the item and `value` will contain the value at index `idx`

Upvotes: 0

Óscar López
Óscar López

Reputation: 236140

You can find out in which index is the element like this:

idx = lst.index(1)

And then access the element like this:

e = lst[idx]

If what you want is the next element:

n = lst[idx+1]

Now, you have to be careful - what happens if the element is not in the list? a way to handle that case would be:

try:
    idx = lst.index(1)
    n = lst[idx+1]
except ValueError:
    # do something if the element is not in the list
    pass

Upvotes: 0

dhwthompson
dhwthompson

Reputation: 2509

First off, I would strongly suggest reading through a beginner’s tutorial on lists and other data structures in Python: I would recommend starting with Chapter 3 of Dive Into Python, which goes through the native data structures in a good amount of detail.

To find the position of an item in a list, you have two main options, both using the index method. First off, checking beforehand:

numbers = [2, 3, 17, 1, 42]
if 1 in numbers:
    index = numbers.index(1)
    # Do something interesting

Your other option is to catch the ValueError thrown by index:

numbers = [2, 3, 17, 1, 42]
try:
    index = numbers.index(1)
except ValueError:
    # The number isn't here
    pass
else:
    # Do something interesting

One word of caution: avoid naming your lists list: quite aside from not being very informative, it’ll shadow Python’s native definition of list as a type, and probably cause you some very painful headaches later on.

Upvotes: 1

Related Questions