-3

i am new to python.

Question is write a function that takes a list of 10 items and deletes 5th and 7th element.The function should convert remaining list into tuple.

I have tried by dint got output:

x = int(raw_input("Please Enter The Number: "))

items = [raw_input('Enter The List of Items: ' ) for i in range(x)]

del items[5:7]

print items

Please help me to solve this problem.

EdChum
  • 376,765
  • 198
  • 813
  • 562

5 Answers5

1

You can use a list comprehension:

print([items[i] for i in range(len(items)) if i not in (4,6)])

List comprehensions are well documented on python.org. "A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses."

The result will be a new list, so if memory matters, then simply delete the items in the right order; i.e. in reverse order:

del items[6]
del items[4]

And finally in your particular case you could have done:

del items[4:7:2]

The slice function is well documented on python.org.

tommy.carstensen
  • 8,962
  • 15
  • 65
  • 108
1
x = int(raw_input("Please Enter The Number: "))

items = [raw_input('Enter The List of Items: ' ) for i in range(x)]

del items[6]
del items[4]


print items
Sailesh Kotha
  • 1,939
  • 3
  • 20
  • 27
1

Delete items from list with

del items[x] # x - list index, starting with 0

While

del items[x:y]

will delete the slice of list, its not what you are looking for. So, just call del twice, with index needed. To create tuple from list, just use

new_var = tuple(items)
Slam
  • 8,112
  • 1
  • 36
  • 44
1

You were supposed to return a tuple of the sequence given with 5th and 7th removed:

def fifth_and_seventh_removed(lst):
    lst = list(lst)
    del lst[6]
    del lst[4]  # order matters!
    return tuple(lst)

If you do del lst[4] followed by del lst[6], you will remove the fifth and eighth element, clearly (the why part is left as an exercise for the reader).


Alternatively for a functional approach use enumerate, with generator expression:

def tuple_with_elements_removed(l, to_remove=(5, 7)): 
    """Return a copy of a iterable with certain elements removed
       (whose ordinals are in the to_remove); by default 
       5th and 7th elements are removed:"""

    to_remove = set(to_remove)
    return tuple(e for i, e in enumerate(l, 1) if i not in to_remove)

now,

>>> print(remove_elements(range(1, 10))
(1, 2, 3, 4, 6, 8, 9) 
0
  1. Delete highest index from the list.
  2. use tuple() to convert list to tuple.

e.g.

code:

x = int(raw_input("Please Enter The Number: "))
items = [raw_input('Enter The List of Items: ' ) for i in range(x)]
print "User Input:-", items
del items[6]
print "After deleting 7th element:-", items
del items[4]
print "After deleting 5th element:-", items
items = tuple(items)
print "Tuple:-", items

output:

:~/study$ python test.py 
Please Enter The Number: 10
Enter The List of Items: 1
Enter The List of Items: 2
Enter The List of Items: 3
Enter The List of Items: 4
Enter The List of Items: 5
Enter The List of Items: 6
Enter The List of Items: 7
Enter The List of Items: 8
Enter The List of Items: 9
Enter The List of Items: 10
User Input:- ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
After deleting 7th element:- ['1', '2', '3', '4', '5', '6', '8', '9', '10']
After deleting 5th element:- ['1', '2', '3', '4', '6', '8', '9', '10']
Tuple:- ('1', '2', '3', '4', '6', '8', '9', '10')
Vivek Sable
  • 9,938
  • 3
  • 40
  • 56