Why slice [::-1] is the same as [-1::-1]? [duplicate] - python

This question already has answers here:
How to explain the reverse of a sequence by slice notation a[::-1]
(7 answers)
Understanding slicing
(38 answers)
Closed 8 months ago.
I just wonder why slice [::-1] is the same as [-1::-1]?
Syntax for reference: [start:stop:step]
It's interesting because almost every Internet article about Python slices states that start defaults to 0. But it seems start defaults to -1 when a step is a negative number.
I didn't find that information in the official Python Docs. Could someone clarify this?
Example:
lst = ['c', 'a', 't']
print(lst[0::-1])
print(lst[::-1])
print(lst[-1::-1])
Output:
['c']
['t', 'a', 'c']
['t', 'a', 'c']

Related

Append list to itself in python [duplicate]

This question already has answers here:
What do ellipsis [...] mean in a list?
(5 answers)
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed 2 years ago.
When I have a list, say mylist = ['a', 'b', 'c'] and I try to append it to its self
mylist.append(mylist)
I get
['a', 'b', 'c', [...]]
Which is a recursive list, for example, mylist[3][3][1] outputs 'b'
I expected to get
['a', 'b', 'c', ['a', 'b', 'c']]
My first question is how can I get what I expected (if there are many methods I would prefer the most performant).
My second question, is what's the reason for this "unexpected" behaviour.
Thanks.
I use python3.8.2
You can use list comprehensions:
mylist.append([x for x in mylist])

Combination of list of list elements [duplicate]

This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
How to concatenate (join) items in a list to a single string
(11 answers)
Closed 4 years ago.
I want a list which is a combination of list of list elements
For example:
my input
x = [['P'], ['E', 'C'], ['E', 'P', 'C']]
The output should be
['PEE','PEP','PEC','PCE','PCP','PCC']]
Any help is highly appreciated.
Use itertools
[''.join(i) for i in itertools.product(*x)]
Note: assuming that last one should be 'PCC'
here is a solution
def comb(character_list_list):
res = ['']
for character_list in character_list_list:
res = [s+c for s in res for c in character_list]
return res
On your example, it gives, as expected
>>> comb([['P'], ['E', 'C'], ['E', 'P', 'C']])
['PEE', 'PEP', 'PEC', 'PCE', 'PCP', 'PCC']
A shorter version is possible using functools.reduce(), but the use of this function is not recommanded.

Allocate each five elements of an array to a block [duplicate]

This question already has answers here:
How to iterate over a list in chunks
(39 answers)
Closed 5 years ago.
I have 15 elements in an array and I want to allocate each five of them to a block respectively. The elements are:
elements=["a",'b','c','d','e','f','g','h','i','j','k','l','m','n','o']
I wanted to say first 5 elements are belonged to block#1, second five are belonged to block#2 and so on. I need to do it in a loop structure as later on, I need to use the information of each block for a special task. As I am new to python I don't know how to write it. Any advice will be highly appreciated.
You can simply use list comprehension for that:
result = [elements[i:i+5] for i in range(0,len(elements),5)]
which will generate:
>>> result
[['a', 'b', 'c', 'd', 'e'], ['f', 'g', 'h', 'i', 'j'], ['k', 'l', 'm', 'n', 'o']]
Or more generic:
def blockify(elements,n=5):
return [elements[i:i+n] for i in range(0,len(elements),n)]
and then call it with blockify(elements,5).
What we do is we create a range that ranges from 0 to the len(elements) (length of the elements), and makes hops of 5 (or n in the generic case). Now for each of these steps, we add a slice elements[i:i+5] to the result.
Loop through all and divide index by 5 (floor division operator //)
The result of dividing is number of group.

Getting Second-to-Last Element in List [duplicate]

This question already has answers here:
How do I get the last element of a list?
(25 answers)
Closed 6 years ago.
I am able to get the second-to-last element of a list with the following:
>>> lst = ['a', 'b', 'c', 'd', 'e', 'f']
>>> print(lst[len(lst)-2])
e
Is there a better way than using print(lst[len(lst)-2]) to achieve this same result?
There is: negative indices:
lst[-2]

Python - removing one instance of a value in two lists [duplicate]

This question already has answers here:
Loop "Forgets" to Remove Some Items [duplicate]
(10 answers)
Remove all occurrences of a value from a list?
(26 answers)
Closed 7 years ago.
Currently, I want to remove a value that is being looped through if it exists in both lists, as such:
for value in Word1:
if value in Word2:
Word1.remove(value)
Word2.remove(value)
However, this code is returning ['a', 'b', 'b'] for the Word1: ['a', 'a', 'a', 'b', 'b', 'b'] and Word2: ['a', 'a', 'b', 'b'] when I would expect it to return ['a', 'b']. What is causing this issue? Pythontutor's visualisation doesn't seem to be helping me.
You must not change a list, while iterating over it.
First, determine the elements, to be removed, then remove them:
to_be_removed = set(Word1).intersection(Word2)
Word1 = [w for w in Word1 if w not in to_be_removed]
Word2 = [w for w in Word2 if w not in to_be_removed]

Categories