0

This below appends the s to the list l

 s = pd.Series([1], name='foo')
 l = []
 l.append(s)

This only appends 1 to l

s = pd.Series([1], name='foo')
l = list(s)

How to implement the first script the best way without declaring a list and then appending?

7
  • 2
    you mean series.tolist() ? Commented Feb 6, 2019 at 15:18
  • 1
    @anky_91 has it. Use l = s.tolist() Commented Feb 6, 2019 at 15:20
  • I mean avoiding the message "The list creation could be rewritten as a list literal" from compilers. Commented Feb 6, 2019 at 15:21
  • 2
    You could use this: l = [s] Commented Feb 6, 2019 at 15:22
  • 1
    Do you want [s]? or l.append([s])? Commented Feb 6, 2019 at 15:22

1 Answer 1

1

[x] makes a list with x as an element.

list(x) makes a list produced by iterating over x. x has to be iterable, otherwise you'll get an error.

It is, in effect, [i for i in x], or

alist = []
for i in x:
   alist.append(i)
Sign up to request clarification or add additional context in comments.

Comments

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.