1

I have the following python list in

[0,1,2,'def','ghi']

Now I want to convert above into another list of tuples by discarding first list item so for e.g. I want [(1,0),(2,1),('def',2),('ghi',3)]

I have the following code

point = [0,1,2,'def','ghi']
spliltm0 = split[1:]
ls = ()
int i = 0
for a in spliltm0:
   ls = (a,i++)

The above seems to be long code for Python, is there any shorter version of above code? I am super new to Python.

3
  • 1
    strip() on a list? Also [(j,i) for i,j in enumerate(point[1:])] Commented Jul 19, 2015 at 19:04
  • Sorry it was typo please see edited question Commented Jul 19, 2015 at 19:08
  • I am a beginner in python I mentioned in the question down voters care to tell the reason Commented Jul 19, 2015 at 19:11

2 Answers 2

3

That code isn't Python at all, since you do int i and i++, neither of which are Python*; also, strip() and split() are methods on strings, not lists.

Your result can be achieved in a one-line list comprehension:

result = [(elem, i) for i, elem in enumerate(point[1:])]

* i++ is syntactically valid in Python, but doesn't at all do what you think it does.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to be clear about the basics of Python before asking in the Python Forum.

1) You cannot apply strip function on a 'list' object, It should be string object.

2) spliltm0 = split[1:], Here split is treated as string, and is not defined. Also split is a string method which should not be used as a variable for storing string.

3) int i = 0 This is a format of C/C++. Not applicable to Python

4)

for a in spliltm0:
   ls = (a,i++)

i++ is not available in Python. Refer to this link (Why are there no ++ and --​ operators in Python?)

1 Comment

str.split is a str method not a builtin function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.