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

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]

Related

Function that takes list input, and returns a new list containing only the elements in that are repeated [duplicate]

This question already has answers here:
How do I find the duplicates in a list and create another list with them?
(42 answers)
Closed 5 months ago.
>>> list_a = ['a', 'b', 'c', 'c']
>>> get_repeated(list_a)
['c']
What would be the most pythonic way to do function get_repeated()?
this should work
from collections import Counter
list_a = ['a', 'b', 'c', 'c']
count = Counter(list_a)
output = [key for key, val in count.items() if val > 1]
print(output)
>>> ['c']

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

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']

Create unique cartesian product from a list [duplicate]

This question already has answers here:
How to get all possible combinations of a list’s elements?
(32 answers)
Closed 2 years ago.
How do I create a cartesian product of a list itself with unique elements?
For example, lists = ['a', 'b', 'c'], and I want to create [['a', 'b'], ['a','c'], ['b','c']].
from itertools import combinations
lists = ['a', 'b', 'c']
print(list(map(list, combinations(lists,2))))

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.

Categories