-2

i know i can use the input function in conjunction with the eval function to input a list of numbers:

numbers = eval(input("enter a list of numbers enclosed in brackets: "))

Also, given a list named items, i can get the list of all the elements of items, except the first one, with the expression:

items[1:]

im just not sure how to go about getting the program to do what i want it to do

6
  • 1
    Is this homework? In any case, good job on showing the bits you have figured out so far. It woul dbe even better if you try to work that into the outline of a working programme as far as you can, and post that.
    – Marcin
    Jul 29, 2012 at 17:32
  • Can you elaborate the question ? I am trying hard to understand what exactly you are asking for. Jul 29, 2012 at 17:32
  • 2
    Don't use eval() for user input. It's VERY DANGEROUS. An alternative way to do it would be for the user to separate the numbers by spaces. Then just do: numbers = [int(i) for i in input("...").split()] Jul 29, 2012 at 17:36
  • @JoelCornett: unless the OP is using Python 3, input is no safer than eval.
    – DSM
    Jul 29, 2012 at 17:45
  • 1
    @JoelCornett: you could be right, but idioms from 3 which work in 2 are often used in 2, and I tend to assume everyone's using 2 unless told otherwise. Probably a habit it's time to break.
    – DSM
    Jul 29, 2012 at 17:59

4 Answers 4

4

If you have a list and you want to know if the first value appears again later in the list, you can use:

items[0] in items[1:]

That will return True or False depending on whether the first element in items appears again later in items.

9
  • This is elegant, and works. However, it results in copying of the list (the expression items[1:] results in a new list, rather than a proxy). For larger lists, this may perform poorly. That is unlikely to be an issue here.
    – Marcin
    Jul 29, 2012 at 17:37
  • 1
    Agreed. If performance is an issue, an alternative would be (items.count( items[0] )) > 1
    – David
    Jul 29, 2012 at 17:39
  • This is also not an elegant solution, you need only to know if there is one more item , you don't need to count through the whole list
    – Michael
    Jul 29, 2012 at 17:43
  • 5
    This is far and away the simplest way to do it, and so the right way. If you really had a super-long list that you didn't want to recreate, you could use items[0] in islice(items, 1, None) but that's serious overkill here.
    – DSM
    Jul 29, 2012 at 17:49
  • 3
    Note that if the user is manually inputting a list, the amount of time it would take to copy the list for slicing is probably several orders of magnitude smaller than the time the user takes to enter all the numbers. So performance shouldn't be an issue here.
    – algorowara
    Jul 29, 2012 at 19:51
2

Don't use eval, ast.literal_eval is safer

import ast
numbers = ast.literal_eval(raw_input("enter a list of numbers enclosed in brackets: "))
1
  • Not downvoting, but this should have been a comment to the question imho since this does not answer the question (although it's a useful remark to make)... Jul 29, 2012 at 18:16
2

An easier solution will be

x = l[0]
l[0] = None
print x in l
l[0] = x

The advantage is that you don't need to recreate the list

1

There are two parts to your problem:

  1. get a list of numbers from the user
  2. check if the first number is repeated in this list

There are several ways to get a list of numbers from a user. Since you seem to be new to python, I will show you the easiest way to program this:

n = raw_input("How many numbers in your list?: ")
n = int(n) # assuming the user typed in a valid integer
numbers = []
for i in range(n):
    num = raw_input("enter a number: ")
    num = int(num)
    numbers.append(num)

# now you have a list of numbers that the user inputted. Step 1 is complete
# on to step 2
first_num = numbers[0]
for n in numbers[1:]:
    if n == first_num:
        print "found duplicate of the first number"

Now, there are more elegant ways to accomplish step 1. For example, you could use a list comprehension:

numbers = [int(n) for n in raw_input("Enter a bunch of space-separated numbers: ").split()]

Further, step 2 can be simplified as follows:

if numbers[0] in numbers[1:]:
    print "found duplicates of the first number"

Hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.