This question already has answers here:
What do ellipsis [...] mean in a list?
(5 answers)
Closed 5 years ago.
x = [4, 5, 6]
li = [1, 2, 3, 7]
li.insert(3,x)
x+=li
print(x)
The output is:
[4, 5, 6, 1, 2, 3, [...], 7]
I'm new to python/coding and I don't know what these ellipses are but when I do other code it starts getting weird. Wasn't sure what to ask since I have no clue what's going on. Thank you!
you're inserting a list inside your list, probably not what you want.
Then when doing this
x+=li
the representation of the list then shows an ellipsis because you're referencing the list from itself (x is referenced in li already)
To insert several items at once in a list in-place you could use slice assignment:
>>> x = [4, 5, 6]
>>> li = [1, 2, 3, 7]
>>> li[3:3] = x
>>> li
[1, 2, 3, 4, 5, 6, 7]
Related
This question already has answers here:
Understanding slicing
(38 answers)
Closed 7 months ago.
I am trying to slice a Python list and run into the following error:
>>> a=[2,3,4,5,6,7,8,9]
>>> print(a)
[2, 3, 4, 5, 6, 7, 8, 9]
>>> print(a[0:2])
[2, 3]
>>> print(a[2:2])
[]
>>>
Expected answer: [4,5]
Could someone tell me what I am doing wrong?
EDIT: I figured out my own answer here.
>>> a=[2,3,4,5,6,7,8,9]
>>> print(a)
[2, 3, 4, 5, 6, 7, 8, 9]
>>> print(a[0:2])
[2, 3]
>>> print(a[2:4])
[4, 5]
>>>
n00b mistake!
print(a[2:4])
The second number is end index, not slice length.
This question already has answers here:
What is the difference between `sorted(list)` vs `list.sort()`?
(7 answers)
Closed 1 year ago.
For Example:
a = [1, 3, 5, 4, 2]
a = a.sort()
print(a)
Output:
None
a = [1, 3, 5, 4, 2]
a.sort()
print(a)
Output:
[1, 2, 3, 4, 5]
My question is why does a = a.sort() reslt in None rather than [1, 2, 3, 4, 5]? But without a= it gives me [1, 2, 3, 4, 5].
Thank you
Because, a.sort returns None, and you are overwriting a's value with that None. The list will still be sorted, because list.sort does that in place. sorted(list) does the inverse : It copies the list, sorts that copy, and returns it. You then have to assign the list to it.
This question already has answers here:
Removing duplicates in lists
(56 answers)
One-liner to remove duplicates, keep ordering of list [duplicate]
(6 answers)
Closed 3 years ago.
I have a big list of strings appearing many times and I want a list of the same strings to appear only once.
An example with numbers would be:
a = [1, 2, 2, 3, 4, 4]
and I want to get
b = [1, 2, 3, 4]
What I tried is something like:
a = [1, 2, 2, 3, 4, 4]
[x for x in a if a.count(x) == 1]
[1, 3]
but this omits the duplicate numbers and takes only those appearing once.
You can try this:
import collections
a = [1, 2, 2, 2, 3, 3, 4, 4, 5, 6, 7, 7, 8]
print([item for item, count in collections.Counter(a).items()])
This question already has answers here:
python: changes to my copy variable affect the original variable [duplicate]
(4 answers)
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 4 years ago.
As I don't know what title should be given to my this confusion so I'm putting it just a doubt
a = [1,2,3,4,5]
b = a
for i in range(len(a)):
c = (i - 4)
print(a)
print(b)
b[c] = a[i]
print(a)
print(b)
output
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 1, 3, 4, 5]
[1, 1, 3, 4, 5]
[1, 1, 3, 4, 5]
[1, 1, 3, 4, 5]
[1, 1, 1, 4, 5]
[1, 1, 1, 4, 5]
...
why values of list a is getting in each step of loop?
Your problem lies in this line:
b = a
This doesn't do what you think it does. In particular, it does not make a copy of a. After the assignment, both b and a refer to the same object. Thus, any change to b is reflected in a also.
One way to force a shall copy is to use the slice syntax:
b = a[:]
This question already has answers here:
Python list rotation [duplicate]
(4 answers)
Closed 8 years ago.
Suppose I have a list u = [1, 2, 3, 4, 5], and u[1:] returns [2, 3, 4, 5].
I wonder what indexing returns [2, 3, 4, 5, 1], going from the second position to the last and then the first?
You can make a general function that does this at any point in your list, just by adding two slices. This was an intentional design as to why slicing is half-open (includes left index, but excludes right index)
def rotate(l, i):
return l[i:] + l[:i]
>>> u = [1, 2, 3, 4, 5]
>>> rotate(u, 1)
[2, 3, 4, 5, 1]
>>> rotate(u, 2)
[3, 4, 5, 1, 2]