-6

ok looks like many did not understand the question. i will make it more clear.

i got a list:

List_1 = [1,2,3,4,5,6,7,8,9]

and what i do in my program is that if i press 1 that will make List_1[0] into X or what ever number i press, it turns it into an X. my list has 9 numbers total.

What i want to do, is that IF 3 specific numbers are converted into X then the program will move on.

so if i end up with:

List_1 = [1,'X',3,4,'X',6,'X',8,9]

then the program will move on.

John Kugelman
  • 330,190
  • 66
  • 504
  • 555
  • 1
    `list2 = a[0:3]`, or `list = a[:-1]` – tobias_k Oct 23 '13 at 14:04
  • or even `list2 = a[:3]` – devnull Oct 23 '13 at 14:05
  • 1
    This looks like it could be of help to you: http://stackoverflow.com/questions/509211/pythons-slice-notation –  Oct 23 '13 at 14:06
  • 1
    Thanks to the comments it seems obvious now you're after getting items from a list - but still not sure where the *string* in the title comes into this? – Jon Clements Oct 23 '13 at 14:07
  • 1
    After the most recent update, this looks like a homework problem and the OP has not made minimal effort on understanding how to access items in a `list`. – ely Oct 23 '13 at 14:41

2 Answers2

3

If you need a contiguous set (such as the first three entries in your original question) use slice syntax:

list_2 = a[:3]

If you need only those elements from a specific set, use a comprehension:

stuff_i_need = [1, 'gg']
list_2 = [x for x in L if x in stuff_i_need]

(although, if you know what you need and it is a very small list, then just manually accessing the locations in the list that contain the elements you need is fine).

If you want to make a string of some contents of the list, one option is to just concatenate them yourself and wrap the elements with the string constructor str:

str(L[0]) + str(L[3])

Another way to do the same thing is:

import operator
reduce(operator.add, map(str, L))

# Also, just replace L with any of the slicing or accessing mentioned above.
ely
  • 70,012
  • 31
  • 140
  • 215
  • Can you elaborate? Your comment does not make any sense to me. – ely Oct 23 '13 at 14:31
  • If you want `[1,3,5]` then just access the locations for those elements. `list2 = List[0:1] + List[2:3] + List[4:5]`. If you don't know the indices for grabbing the elements you want, but you do know what the elements are, then look at @Michael Brennan's answer. However, if you know which elements you want already, then by definition (unless there are repeats) don't you already have the sub-list you're looking for? – ely Oct 23 '13 at 14:34
  • Just as a side note: editing an answer is not a place for leaving a comment. Only edit the answer if you are suggesting an improvement in its functionality as an answer. Don't try to remove answers by editing them, deleting all of their text, and over-writing it with a message that you re-worded your original question. Just leave a comment. – ely Oct 23 '13 at 14:41
  • If you want [1,3,5] then just access the locations for those elements. list2 = List[0:1] + List[2:3] + List[4:5]. The text above game me the answer, but i wonder why is it List[0:1] and [2:3] and not just List[0] + List[3]? what dose the "2:3" mean? – user2911693 Oct 23 '13 at 14:50
  • `List[0]` is an element of the list, not a sublist. `List[x:y]` is the *sublist* consisting of any potential elements starting at `x` and going to `y-1`. That can be the empty list if those indexes are out of range for the list. – ely Oct 23 '13 at 17:14
  • I would also welcome any explanation about why you un-accepted the answer. If it helped you solve the problem, consider accepting it (clicking the check mark) or letting us know what remaining issues there are. – ely Oct 23 '13 at 17:16
  • [Dissolve the question.](http://lesswrong.com/lw/of/) – ely Oct 24 '13 at 12:57
1

Use a slice, as mentioned the other answers and comments. But if the indices are non-contiguous you can use a list comprehension together with enumerate:

>>> [x for i, x in enumerate(a) if i in [0, 1, 3]]
[1, 'X', 'X']

Update

The question changed, asking instead how to take different parts of a list and join them into a string. A small modification of the above:

>>> "".join(str(x) for i, x in enumerate(L) if i in [0, 3])
'1gg'
Michael Brennan
  • 1,951
  • 12
  • 17