diff --git a/django/db/migrations/loader.py b/django/db/migrations/loader.py index b0f22a6734..8a1cf431bd 100644 --- a/django/db/migrations/loader.py +++ b/django/db/migrations/loader.py @@ -156,9 +156,9 @@ class MigrationLoader: if key[0] in self.migrated_apps: try: if key[1] == "__first__": - return list(self.graph.root_nodes(key[0]))[0] + return self.graph.root_nodes(key[0])[0] else: # "__latest__" - return list(self.graph.leaf_nodes(key[0]))[0] + return self.graph.leaf_nodes(key[0])[0] except IndexError: if self.ignore_no_migrations: return None diff --git a/django/db/migrations/questioner.py b/django/db/migrations/questioner.py index 9b5b9f3510..7229ca2dba 100644 --- a/django/db/migrations/questioner.py +++ b/django/db/migrations/questioner.py @@ -48,7 +48,7 @@ class MigrationQuestioner: elif hasattr(migrations_module, "__path__"): if len(migrations_module.__path__) > 1: return False - filenames = os.listdir(list(migrations_module.__path__)[0]) + filenames = os.listdir(migrations_module.__path__[0]) return not any(x.endswith(".py") for x in filenames if x != "__init__.py") def ask_not_null_addition(self, field_name, model_name): diff --git a/django/db/models/base.py b/django/db/models/base.py index ccc838350b..0839464446 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -481,8 +481,8 @@ class Model(metaclass=ModelBase): del kwargs[prop] except (AttributeError, FieldDoesNotExist): pass - if kwargs: - raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0]) + for kwarg in kwargs: + raise TypeError("'%s' is an invalid keyword argument for this function" % kwarg) super().__init__() post_init.send(sender=cls, instance=self) diff --git a/django/db/models/query.py b/django/db/models/query.py index 1b3c2629d6..81b479daa3 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -275,7 +275,8 @@ class QuerySet: qs = self._clone() qs.query.set_limits(k, k + 1) - return list(qs)[0] + qs._fetch_all() + return qs._result_cache[0] def __and__(self, other): self._merge_sanity_check(other) @@ -548,17 +549,13 @@ class QuerySet: def first(self): """Return the first object of a query or None if no match is found.""" - objects = list((self if self.ordered else self.order_by('pk'))[:1]) - if objects: - return objects[0] - return None + for obj in (self if self.ordered else self.order_by('pk'))[:1]: + return obj def last(self): """Return the last object of a query or None if no match is found.""" - objects = list((self.reverse() if self.ordered else self.order_by('-pk'))[:1]) - if objects: - return objects[0] - return None + for obj in (self.reverse() if self.ordered else self.order_by('-pk'))[:1]: + return obj def in_bulk(self, id_list=None, *, field_name='pk'): """ diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index b691fc424b..48bbd7c851 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -217,7 +217,8 @@ class Query: @cached_property def base_table(self): - return list(self.alias_map)[0] if self.alias_map else None + for alias in self.alias_map: + return alias def __str__(self): """ diff --git a/django/forms/utils.py b/django/forms/utils.py index 587cea85a3..9bdaffa44b 100644 --- a/django/forms/utils.py +++ b/django/forms/utils.py @@ -95,7 +95,7 @@ class ErrorList(UserList, list): def get_json_data(self, escape_html=False): errors = [] for error in self.as_data(): - message = list(error)[0] + message = next(iter(error)) errors.append({ 'message': escape(message) if escape_html else message, 'code': error.code or '', @@ -133,7 +133,7 @@ class ErrorList(UserList, list): def __getitem__(self, i): error = self.data[i] if isinstance(error, ValidationError): - return list(error)[0] + return next(iter(error)) return error def __reduce_ex__(self, *args, **kwargs): diff --git a/django/templatetags/i18n.py b/django/templatetags/i18n.py index 8627bf4331..517eb61528 100644 --- a/django/templatetags/i18n.py +++ b/django/templatetags/i18n.py @@ -500,7 +500,7 @@ def do_block_translate(parser, token): options[option] = value if 'count' in options: - countervar, counter = list(options['count'].items())[0] + countervar, counter = next(iter(options['count'].items())) else: countervar, counter = None, None if 'context' in options: diff --git a/tests/get_earliest_or_latest/tests.py b/tests/get_earliest_or_latest/tests.py index c59f4324f0..eaa318663f 100644 --- a/tests/get_earliest_or_latest/tests.py +++ b/tests/get_earliest_or_latest/tests.py @@ -155,7 +155,7 @@ class TestFirstLast(TestCase): # We know that we've broken the __iter__ method, so the queryset # should always raise an exception. with self.assertRaises(IndexError): - IndexErrorArticle.objects.all()[0] + IndexErrorArticle.objects.all()[:10:2] with self.assertRaises(IndexError): IndexErrorArticle.objects.all().first() with self.assertRaises(IndexError):