Skip to main content

All Questions

Tagged with
-3 votes
1 answer
71 views

How to remove duplicates from a list in Python while preserving order? [duplicate]

I'm trying to remove duplicate items from a list in Python, but I want to preserve the original order of the elements. For example: my_list = [1, 3, 2, 3, 1, 5, 2] I tried using set() to remove ...
sahzia muno's user avatar
-5 votes
1 answer
77 views

Separating/Adding lists together for a 2D array [closed]

I have a list of numbers and need to analyze them in two different ways. [[ 0, 1, 0, 1, 0], [1, 1, 1, 1, 1], [ 0, 1, 1, 1, 0]] I need to add up any 1's and group them but ONLY if they're after each ...
kbrocks100's user avatar
1 vote
3 answers
89 views

Generating all length-N combinations of all length-M sublists of a list, without repetitions

I have a long input list, and I want to generate combinations of 3 elements. But each combination's elements should be drawn from a length-5 sublist of the original list, rather than drawing elements ...
tim pain's user avatar
-1 votes
0 answers
33 views

How to delete element from python list in loop over this list [duplicate]

I have time segments in list such as: [{'start': 12.4, 'end': 12.8}, {'start': 14.0, 'end': 15.5}, {'start': 15.7, 'end': 16.2}, {'start': 16.5, 'end': 17.1}, {'start': 17.9, 'end': 18.5}, {'start': ...
Anthony sk's user avatar
-2 votes
0 answers
36 views

"What's the difference between using append() vs extend() when working with Python lists?" [duplicate]

I'm currently learning Python and working with lists. I came across two methods—append() and extend()—and I'm a bit confused about how they're different. For example: a = [1, 2, 3] b = [4, 5] a....
RAJ chaudhari's user avatar
-4 votes
0 answers
49 views

How can I convert a list of numbers in various decimals to two decimal places [duplicate]

I have a code that calculate the growth of population as follows: new_pop = init_pop new_pop_list = [new_pop] while new_pop <= 1500: new_pop *= (1 + growth_rate) new_pop_list.append(round(...
Nemo's user avatar
  • 1,268
1 vote
0 answers
58 views

Is it possible to use (python) list comprehension in this case? [duplicate]

a = [] for b in c: try: a.append(SomeClass.SomeMethod(doc,b).Name) except: a.append(None) Chat GPT suggested this.. but it fails to work a = [ SomeClass.SomeMethod(doc,b)....
DoYouLikeHam's user avatar
-2 votes
0 answers
34 views

Pythonic way to create a list of repeated different mutable objects [duplicate]

I want a list of five lists: c = [[],]*5 but unfortunately: c[0].append(1) print(c) gives this unwanted result: [[1], [1], [1], [1], [1]] is there a way better than: [[] for _ in range(5)] enter ...
jimifiki's user avatar
  • 5,558
5 votes
2 answers
153 views

Algorithm for detecting full loop when iterating over a list

Assignment: Write a funcion cycle_sublist(lst, start, step) where: lst is a list start is number that satisfies: 0 <= start < len(lst) step is the amount we increase your index each iteration ...
David's user avatar
  • 382
1 vote
1 answer
90 views

Using a list of values to select rows from Polars DataFrame

I have a Polars DataFrame below: import polars as pl df = pl.DataFrame({"a":[1, 2, 3], "b":[4, 3, 2]}) >>> df a b i64 i64 1 4 2 3 3 2 I can subset ...
GH KIM's user avatar
  • 316
-2 votes
0 answers
33 views

Indirectly calling a list In Python [duplicate]

I want to fill a list by indirectly calling another list (it's called "indirect addressing" in PLCs, not sure what it's called in Python). The name of the list I want to fill is Factor_Data. ...
bilkoboyo's user avatar
4 votes
1 answer
103 views

Why does sort() with key function not do anything while sorted() is working?

I have a list of integers with duplicates and I need to sort it by the number of these duplicates. For example: input: n = [2, 4, 1, 2] output: n = [4, 1, 2, 2] I wrote some code and noticed, that ...
Ksuvo's user avatar
  • 43
0 votes
3 answers
86 views

How best to insert a separator between list elements (but not at the beginning or end) in Python? [duplicate]

I'm a beginner, practicing Python, I want to understand if one or the other way is better version. If yes, please elaborate! And how to determine the same in future, on my own? words = ['This', 'is', '...
pineapplepizza's user avatar
-5 votes
1 answer
53 views

Fastest and cleaner ways to insert, update, delete 2-dimensional array [duplicate]

I'm newbie in python language and have some prolem here. I have a list of name and score for example: score = [['Andy', 80], ['John', 70], ['Diana', 100]] I want to Delete or Update score based on it'...
Tommy Sayugo's user avatar
-1 votes
1 answer
48 views

Use pop function to catch a value and then add them

I was using pop function to catch a value and then add them as below my_list = [1,2,3,4,5] a = my_list.pop(0) b = my_list.pop(0) c = a+b print(c) I was expecting the output as 2 but it shows it as ...
mindbuzzz's user avatar

15 30 50 per page
1
2 3 4 5
4764