is index same for same elements? [duplicate] - python

This question already has answers here:
Index of duplicates items in a python list
(21 answers)
Closed 1 year ago.
nums = [11, 2,4, 2, 5]
for i in nums:
print(nums.index(i),i )
I run the above code and it uses same index for similar elements(here 2 at index 1 and 3).
I wasn't aware of this python list behavior, does it mean the use of lists can be restrictive for similar elements in same list?

The index() method returns the position at the first occurrence of the specified value.
So it returns first index of number 2.
nums = [11, 2,4, 2, 5]
You can enumerate() to get all indexes.
nums = [11, 2,4, 2, 5]
for i,n in enumerate(nums):
print(i, n)
0 11
1 2
2 4
3 2
4 5

Related

Python - Iterate over 2 lists of a different length in a specific way [duplicate]

This question already has answers here:
How to zip two differently sized lists, repeating the shorter list?
(15 answers)
Closed 7 months ago.
I have 2 lists of a different length
list_1 = [1, 2, 3, 4, 5]
list_2 = ['a', 'b']
If I'd do:
for (i,j) in itertools.zip_longest(list_1, list_2):
print (i,j)
The output would be
1 a
2 b
3 None
4 None
5 None
What I want is to iterate over those 2 lists, but repeat the shorter list when it gets exhausted any amount of times while the longer list still iterates. In this example I'd want output to be
1 a
2 b
3 a
4 b
5 a
There's an itertools for that. You want to cycle the second list and do a vanilla zip on the first. cycle will remember and reemit values from list_2 and zip will stop at the end of list_1.
>>> import itertools
>>> list_1 = [1, 2, 3, 4, 5]
>>> list_2 = ['a', 'b']
>>> for i,j in zip(list_1, itertools.cycle(list_2)):
... print(i, j)
...
1 a
2 b
3 a
4 b
5 a
if you want the result to always be the longer of the two lists (either could cycle), you'd need to choose which one uses itertools.cycle. There are a dozen ways to do that, but here's one
>>> zipper = zip(list_1, itertools.cycle(list_2)) if len(list_1) >= len(list_2) else zip(itertools.cycle(list_1), list_2)
>>> for i, j in zipper:
... print(i, j)
...
1 a
2 b
3 a
4 b
5 a
And if you want something that works for iterators in general (they wouldn't know their size in advance), you could make them into a list first.

Why does only 3 values are pop from this code? [duplicate]

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 1 year ago.
numbers = [2,4, 6,8, 11, 23]
popped_numbers = []
for number in numbers:
i = numbers.pop(0)
print(i)
popped_numbers.append(i)
print(numbers)
results
2
4
6
[8, 11, 23]
It is because when you are poping from numbers, then the size of it is actually changing. At the same time the iterator starts from 0 index in the array and increasing. So after picking the first 3 numbers, size of numbers is equal to 3 and iterator is also 3 and the for loop breaks. You can implement it like this:
numbers = [2,4, 6,8, 11, 23]
popped_numbers = []
while len(numbers) > 0:
i = numbers.pop(0)
print(i)
popped_numbers.append(i)
print(numbers)
print(popped_numbers)
Because the pointer, when you pop an element from the numbers list in the loop, the pointer after the third iteration is in third element, at this point the last element in the list.
Like:
Iteration | element | list
__________|_________|_______
1 | 2 | [4,6,8,11,23]
2 | 4 | [6,8,11,23]
3 | 6 | [8,11,23]

Change initial element of a list keeping the sequence [duplicate]

This question already has answers here:
Efficient way to rotate a list in python
(27 answers)
Circular list iterator in Python
(9 answers)
Closed 2 years ago.
I have an algorithm that outputs a list in a specific order, for example:
[0 4 3 2 1 5]
I want to reorder the list to start with the element '1' and keep the sequence, so my output would be:
[1 5 0 4 3 2]
I've searched and tried different possibilities but I'm still struggling with it.
How can I make this work?
lst = [0, 4, 3, 2, 1, 5]
to rotate it into position:
i = lst.index(1)
lst = lst[i:] + lst[:i]

Don't understand double list lookups [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I can't figure out the output of this code:
lst = [3,1,4,-2]
print(lst[lst[-1]])
# output 4
or
lst = [3,1,4,-2]
print(lst[lst[3]])
# output 4
lst[0] = 3
lst[lst[0]] = lst[3] = -2
lst[-4] = 3
lst[lst[-4]] = lst[3] = -2
list=[3,1,4,-2]
print(list[list[0]])
-2
Explanation:
list[0] = 3
substitute this value in list[list[0]] which makes it list[3]
finally print(list[3]) whose output will be -2
list=[3,1,4,-2]
print(list[list[-4]])
-2
Explanation:
list[-4] is 3
substitute this value in list[list[-4]] which makes it list[3]
finally print(list[3]) whose output will be -2
Think about it this way: lst[-4] and lst[0] just return numbers (-2 and 3, respectively).
Thus,
lst = [3,1,4,-2]
print(lst[lst[0]])
is no different than
lst = [3,1,4,-2]
print(lst[3])
Consider the second example,
lst = [3,1,4,-2]
print(lst[lst[-4]])
lst[-4] returns 3, the index value of -1 gives the last element, and -2 gives the second last element of an array and likewise, -4 returns the 4th last element.
Since lst[-4] returns 3 we have print(lst[3]) which returns third indexed element of the array, which is -2, considering 0 being first element.
In the first example, it goes like above where final output should be -2
We can use the index operator [] to access an item in a list. Index starts from 0. So, a list having 5 elements will have an index from 0 to 4. Trying to access an element other that this will raise an IndexError.
Python also allows negative indexing for its sequences. The index of -1 refers to the last item, -2 to the second last item and so on.
From your example
lst = [3, 1, 4, -2]
0 1 2 3 # +ve index
-4 -3 -2 -1 # -ve index
>>> print(lst[lst[0]]) # lst[0] is 3 ==> lst[3] is -2
-2
>>> print(lst[lst[-4]]) # lst[-4] is 3 ==> lst[3] is -2
-2

Can't delete not last single object [duplicate]

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 6 years ago.
My code should recieve a list of numbers and then output on the screen the only numbers which repeat more then once. I don't know why but it don't work with the numbers in the middle of list. My code:
a = [int(i) for i in (input().split())]
a.sort()
for number in a:
if a.count(number)==1:
a.remove(number)
else:
a.remove(a.count(number)-a.count(number)+number)
for number in a:
print(number, end=' ')
I tried changing if on while on 4th string, but then the last number is left in the list.
It should work like:
Sample Input 1: 4 8 0 3 4 2 0 3 Sample Output 1: 0 3 4
Sample Input 2: 10 Sample Output 2:
Sample Input 3: 1 1 2 2 3 3 Sample Output 3: 1 2 3
Sample Input 4: 1 1 1 1 1 2 2 2 Sample Output 4: 1 2
You could approach this problem using set instead:
a = list(map(int, input().split()))
print(" ".join(map(str, set(i for i in a if a.count(i) > 1))))
Explanation:
Firstly, it looks like you should read up on the map function. Instead of a = [int(i) for i in (input().split())], you can just use list(map(int, input().split())), which is much more Pythonic.
Secondly, the important part of the second line is set(i for i in a if a.count(i) > 1), which just creates a new list containing only the duplicates from a (i.e. [1, 2, 3, 2, 3] becomes [2, 3, 2, 3] and then applies set to it, which converts [2, 3, 2, 3] into {2, 3} (which is a set object).
In case you're wondering what map(str, ...) is for, it's so that you can print each of the elements inside your new set (e.g. {2, 3}).
You can use built-in lib collections to count list items and filter it by a required condition.
import collections
ints = map(int, input().split())
count = collections.Counter(ints)
print filter(lambda x: count[x] > 1, count)

Categories